LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-19-2007, 03:35 AM   #1
guarriman
Member
 
Registered: Nov 2004
Posts: 101

Rep: Reputation: 15
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?
 
Old 01-19-2007, 09:18 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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
 
Old 01-19-2007, 02:51 PM   #3
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
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.

Last edited by haertig; 01-19-2007 at 02:55 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I get sudo to work? Efwis Slackware 6 12-03-2006 10:19 AM
sudo does not work powah Linux - Security 1 12-06-2005 04:49 PM
sudo doesn't work oomfoofoo Debian 3 08-13-2005 07:34 AM
sudo doesn't work mla Linux - Software 1 10-01-2003 08:24 PM
sudo doesn't work mla Linux - Software 4 10-01-2003 07:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration