-   Linux - Software (
-   -   My cron doesn't work but 'sudo -u john' does (

guarriman 01-19-2007 04:35 AM

My cron doesn't work but 'sudo -u john' does

Using Fedora Core 2, I'm trying to execute a shell script via cron.

These are the contents of the script '/home/john/'
/usr/bin/php /home/john/email.php
/usr/bin/php /home/john/backup.php

My crontab (made with 'crontab -u john crontab.txt')
00 02 * * * /home/john/

At 02:00, the mail is sent (email.php), but the backup is not made (backup.php).

If I do:
[]# sudo -u john /home/john/
it works ok

I chmowned 'email.php', 'backup.php' and 'script.php' to 'john'.

Any suggestion?

Guttorm 01-19-2007 10:18 AM


When you execute things with cron, you don't get all the environment variables you get when you log inn normally. My guess is the PATH variable, so here's my suggestion:

log in as "john"
type "echo $PATH" and copy the entire value
type crontab -e
In the beginning of the crontab file, add setting the PATH, e.g.:

haertig 01-19-2007 03:51 PM

I have troubleshot many many many cronjob failures over the years. In the case where it runs when manually invoked from the commandline, but not when invoked from cron, it has ALWAYS turned out to be an environment problem or permissions problem, just like Guttorm has already said.

From a command prompt, as the userid that you want to run the cronjob from, run "env" and trap it's output to a file. Then create a cronjob using this same userid and have it run "/usr/bin/env > /tmp/my.cron.env". Notice that I put the fully qualified pathname to the "env" command in that cronjob. /usr/bin would probably(?) be in a normal cronjob's environment, but why take chances? It's good form to be 100% explicit when messing with cronjobs.

After the cronjob has executed, compare the two environment files. Look for something that is missing that your job might require.

All times are GMT -5. The time now is 01:05 PM.