LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   My cron doesn't work but 'sudo -u john foo.sh' does (http://www.linuxquestions.org/questions/linux-software-2/my-cron-doesnt-work-but-sudo-u-john-foo-sh-does-520809/)

guarriman 01-19-2007 03:35 AM

My cron doesn't work but 'sudo -u john foo.sh' does
 
Hi.

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

These are the contents of the script '/home/john/script.sh'
-----
#!/bin/sh
/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/script.sh
-----

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/script.sh
it works ok

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

Any suggestion?

Guttorm 01-19-2007 09:18 AM

Hi

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.:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games

haertig 01-19-2007 02: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 03:25 PM.