问题
I have built one php file to check some result, so that I need to setup a cronjob.
I set one to run every 30 minute, so that the results will be send. However, I don't know why my crontab did not run after every 30 minute.
Here is how I set the crontab:
*/30 * * * * php /var/www/html/result.php
I have confirmed my file directory is correct. What I not sure is about the timing part: isn't it possible to use */30 * * * *
or 30 * * * *
? I set */30 * * * *
and did not work.
回答1:
Given
*/30 * * * * php /var/www/html/result.php
There are multiple possibilities why it is not working:
First of all it is important to check if the simple execution of
php /var/www/html/result.php
. This is required. But unfortunately, accomplishing this does not mean that the problem is solved.The path of the
php
binary has to be added.*/30 * * * * php /var/www/html/result.php
to be changed to
*/30 * * * * /usr/bin/php /var/www/html/result.php
or whatever coming from
which php
.Check the permission of the script to the user running the crontab.
Give execution permission to the file:
chmod +x file
. And make sure the crontab is launched by a user having rights to execute the script. Also check if the user can access the directory in which the file is located.To be safer, you can also add the php path in the top of the script, such as:
#!/usr/bin/php -q <?php ... ?>
Make sure the user has rights to use crontab. Check if he is in the
/etc/cron.d/deny
file. Also, make a basic test to see if it is a crontanb or php problem.* * * * * touch /tmp/hello
Output the result of the script to a log file, as William Niu suggested.
*/30 * * * * /usr/bin/php /var/www/html/result.php > /tmp/result
Use the
-f
option to execute the script:*/30 * * * * /usr/bin/php -f /var/www/html/result.php > /tmp/result
To sum up, there are many possible reasons. One of them should solve the problem.
回答2:
It may be because php is not in the path. crontab
has a very minimal path
. So, include the full path for your php program.
you can test your cron commands by piping the output to a file, e.g.
*/30 * * * * php /var/www/html/result.php > /tmp/result.log
From this reference page, under "Crontab Environment":
cron invokes the command from the user’s HOME directory with the shell, (/usr/bin/sh). cron supplies a default environment for every shell, defining:
HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh
Also, /30
syntax might not be supported by all platforms, so, try to change it to 0,30
instead.
回答3:
Had a similar issue; from command line, it worked, but from cron, no go.
had a "include ("./connect.php"); in my php code for the db stuff.
Removed that, and added the connect.php code directly into the php script, and it worked from cron.
回答4:
I had a similar issue on Ubuntu 14.04.1
and the problem turned out to be the way I was modifying the crontab:
I was using sudo crontab -e
instead of just crontab -e
and this caused my changes to be ignored.
回答5:
I had a funny one regarding this. Although my scripts would run manually, they wouldn't run from crontab.
Turns out that because the script was being run from /usr/bin/php
rather that the location of the file (as it does when I run it manually) my php require
wasn't finding the files I wanted. Changing that to reflect the full address fixed it.
troubleshooting by running the script as /usr/bin/php -f /var/www/myfile.php
helped me find the issue
回答6:
After a day of puzzling why my script would work directly (to send data in an email to a gmail account) I discovered that all the deliberate sends worked when I clicked the url and all the cron sends went into spam. No idea why but I thought I'd share it.
回答7:
Willem's answer showed me the way. In my case, I have a "include("connection.php")" inside my code. I changed connection.php to /my/full/path/connection.php. I have some rename() calls with the relative path, and I changed to the absolute path. That worked for me. I hope it can help someone else.
回答8:
Easy and logical way:
Checking the cron logs at /var/log/cron will give you very useful info
less /var/log/cron
Eg.,
My cron entry is * * * * * /usr/bin/php /cat.php
<== Run cat.php every minute
The log file will contain an entry similar to the one below every time a cron entry is run
Jan 24 08:06:01 OlaTower CROND[13641]: (root) CMD (/usr/bin/php /cat.php)
Jan 24 08:07:01 OlaTower CROND[13641]: (root) CMD (/usr/bin/php /cat.php)
Here, the php command will be executed every minute and there will be an entry in the log file every minute
If the entry is not there then crond is not even picking that cronjob. If the log entry is there and still you are not getting the desired output then there is something wrong with the command/application logic
回答9:
Are you sure it is not running? If you use exec, realize that you are running from cron and the full path for everything is required, so instead of cp
, you'll need to use /bin/cp
.
来源:https://stackoverflow.com/questions/7397469/why-is-crontab-not-executing-my-php-script