PHP script is not being executed by cron - php

I have a very weird issue that I'm gonna pull my hair off..
I have php script ran by cron. When I manually run
php /var/www/html/mypath/mycron.php > output.html
it works fine, the script takes about 3 mins, and I can get the output I need.
But when it's called by cron like this
0 1 * * * /usr/bin/php /var/www/html/mypath/mycron.php > output.html
it does not work, nothing's written to output!
I've tried add -f, doesn't work; I've check php max_execution_time, it's more than enough;
I've check the cron log, it says it ran, but I got nothing in output;
I've checked the permission, the crontab is under the user I manually run, and all the directories in mypath under html are owned by that user, and have rwx for owner, shouldn't have had any permission issue; I've try to create new cron under the same directory just for testing, and it worked...
It's driving me crazy, any idea why this is happening?? Thanks!

Related

PHP script not running from cron

I have the following command:
php /var/www/html/XYZ/api_new/XYZ-API/src/public/fetch_events/fetch_events.php
This runs perfectly from the command line. However, I want it to run as a cron job every 15 mins, so I added the following entry to my crontab:
15 * * * * php /var/www/html/XYZ/api_new/XYZ-API/src/public/fetch_events/fetch_events.php
This however, does not seem to work at all. It does not even show up in my system logs (all my other cron jobs do show up).
Any ideas?
There's not really enough information to answer your question, but here are some things that might help.
Sometimes cronjobs don't have context for where to find these programs, so do which php and replace the full path with the php command.
/var/log/cron should have a record of it running, but if there's a problem, you might not know why. Try appending the following to the end of the cron line to aid in debuging:
>> /tmp/fetch_events.log 2>&1
This file will probably contain some hints as to what's going on when the cron does fire.

run a php page once a day using NAS synology

have a php page called cronEmail in the web folder. It incudes the code to end an email to specific users on the website. I want it to open the page once a day and send the email. The page has only php and MySql code to read the recipients of the email.
I am trying to use Task scheduler in the control panel to run the page. I create a user defined script and in schedule I set the time to a certain time and to only run daily once a day.
In the rum command i have tried numerous ways to run it on the time but every time it just passes and does nothing. an example of what I put in for the script is
/web/cronEmail.php OR
chmod 755 /volume1/web/cronEmail.php
There are only two of a many can anyone point me in the right direction
Thanks a million
Seems like you want a cronjob, in the terminal open cron with:
crontab -e
then at the bottom of the file place this
0 4 * * * php /url/to/folder/cronEmail.php
Not sure if you found another solution, but this is what worked for me. If you put the following in the "Run Command" section, it should work:
php /volume1/web/cronEmail.php
You can also create an error log by doing the following:
php /volume1/web/cronEmail.php>> /volume1/web/errors.log 2>&1
If you don't use the php at the beginning, and open up the error.log file that is created, what you'll see is that the Task Scheduler doesn't seem to know that it's looking for PHP, and doesn't recognize the script.

unix `at` from php script, works in shell but not from web

I am having trouble executing a unix at job command from a php webscript.
the php with the at job looks like this:
$output=exec('echo "/usr/bin/perl /home/dir/test.pl" | /usr/bin/at now +1minute')
This will work fine from the shell command prompt. I can execute the php script, run atq to see that job has been set and the job belongs to root. Then wait and confirm that the test.pl script has run. The test.pl script performs a simple update to a database which I can check to confirm everything has worked.
When I execute the php script from the web, go back to my shell and run atq again. I can see that the job has been placed, this time the job belongs to apache, so everything seems fine. But after waiting for the scheduled time, the test.pl script does not execute which I can confirm after seeing the update to the db has not worked.
I belive this must be a permissions issue, but I am not sure. I tried giving /home/dir and the test.pl script ownerships to apache but this still does not work. I tried createing a symlink to /home/dir in the webroot folder but this did not seem to help either. I do not know apache permissions to well so I could be overlooking something.
here is the directory and test.pl ls -l ouput
/home/dir
drwxrwxr-x 2 apache apache
test.pl
-rwxr-xr-x 1 apache apache
Thanks for any help on this one.
If the job is being enqueued then we can discount the possibility that the apache uid is denied access to 'at'.
This then implies that the job is probably failing to execute (how do you know it is failing to run?)
Since you have root access, the next step would be to look at the mail file for the apache uid - or determine where mail for this uid is sent and look there: from the man page:
The user will be mailed standard error and standard output from his commands,
if any. Mail will be sent using the command /usr/sbin/sendmail
(If you don't find anything there, then you might want to try something like pwd | mail -s test apache or running at with -m to check that mail does get handled as it should)
Normally a daemon owner won't have any shell configured - did you set $SHELL before invoking at? (if this is the case and the mail is getting sent then there will be a corresponding error waiting to be read).
You've checked the permissions on the test.pl file and that does not appear to be the issue (assuming fACLs or SELinux restrictions are not being applied). Have you checked the permissions of the perl executable? Could test.pl be attempting to access other files which it might have permissions on?

Cron Jobs - Doesn't seem to execute

Looking through the internet at examples on how to run a php script from a cron job I've settled on this command:
php -q /home/myuseraccount/public_html/crontest/crontest.php
I tried running this from SSH and had success! But when I try this as a cron job nothing happens. I have it set to run every minute. Nothing happens. It should create a text file with the current time in it:
<?php
$file = '/home/myuseraccount/public_html/crontest/test.txt';
$now = time();
file_put_contents($file, $now, FILE_APPEND | LOCK_EX);
?>
It should also email me. It doesn't. I'm assuming it would email me no matter what, like if there was a failure?
It's not possible cron jobs are somehow disabled is it? - I'd assume I wouldnt even be able to set them up...
Is there any way, like from SSH, to test cron jobs?
I've never successfully been able to get cron jobs to work, so I don't know if it's a server issue or just ignorance.
--EDIT--
My cron log file seems to just show a bunch of LIST's and REPLACE's
Consider checking your logs for cron at /var/log/syslog
Please also make sure you can actually write to the file/directory (chmod permissions). Even further ensure that the crontab userlevel can write to the file/directory as well.
I just tried your file and it does execute fine for me, will attempt to do it as a crontab int he same way as you.
EDIT: Ran it as a cronjob and that worked as well, not sure what the issue is!
Apparently crond wasn't running....
#service crond restart
Stopping crond: cannot stop crond: crond is not running. [FAILED]
Starting crond: [ OK ]
Works now.

Cannot execute crontab command from a php script...from shared host

I'm running an application where I need to execute the cron job, once a form is submitted,contains more than 1 lac records in a loop(its on-demand execution of cron job and deletes the job once finished).
I'm using php, I tried to configure the job through cpanel as a test and it works...the command was.."/ramdisk/bin/php5 -f /home/user/public_html/domain/cron.php -q", it worked fine.
But when I tried to run the same job with the exec() of php, like I wrote this whole command to a file (I do not know where the original crontab file is located otherwise could have written to it, please suggest a way to find its location) and ran that file like "exec("crontab /home/user/cron/Feed_cron"); ", in this cron is not working...
I doubted whether the crontab command will not work for particular user.. I tried it from the "root" user....which is the root user....tried the command via SSH or Putty and the response was "command not found". Please find a solution for this, also please give me an idea whether my concept works fine...
please suggest a way to find its location
Try
whereis crontab

Categories