user420574
user420574

Reputation: 1457

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 which 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!

Upvotes: 4

Views: 2234

Answers (4)

dandrews
dandrews

Reputation: 3035

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

Upvotes: 0

Stuck
Stuck

Reputation: 12292

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.

Upvotes: 1

Exos
Exos

Reputation: 3988

./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

Upvotes: 3

innvo
innvo

Reputation: 131

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.

Upvotes: 1

Related Questions