开发者

Can't execute command from crontab?

I want to update some stuff in my database everyday at 16:00.

So I use crontab which execute command whi开发者_运维问答ch run my file.php which run the update. It works perfectly when I execute the command in the bash but There is a problem with the crontab.

crontab:

00 16 * * * ./etc/cron.daily/maj_cat

maj_cat

php var/www/dev/update.php

Thanks!


./etc/cron.daily/maj_cat is a relative path, and var/www/dev/update.php too, try:

00 16 * * * /etc/cron.daily/maj_cat

and maj_cat:

php /var/www/dev/update.php

To you can do:

00 16 * * * /usr/bin/env php /var/www/dev/update.php


You will want to use the full path to PHP,

type in: whereis php

typically PHP resides at /usr/bin/php

resulting in: /usr/bin/php /var/www/dev/update.php

I find it useful to test a crontab is being executed by outputting to a file, so you know that the cron is actually being executed, something like:

/usr/bin/php /var/www/dev/update.php > output.txt

You will probably be better off putting a forward slash before "var" too as I've shown above.


probably the crondeaemon does not use the PATH variable that is set when you do it by hand. Be sure that php is in the path (in the head of your crontab).

Otherwise you could try using absolut paths in your script.


Cron uses a default profile when it runs cronjobs, which will likely have a different PATH variable than what you use when logged in. You can load your own profile at the beginning of the cronjob, to ensure that the cronjob's environment matches your logged in environment.

You can load your profile in this way:

00 16 * * * ~/.profile; ./etc/cron.daily/maj_cat
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜