Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


  Search this Thread
Old 01-19-2007, 04:35 AM   #1
Registered: Nov 2004
Posts: 101

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

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335

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.:
Old 01-19-2007, 03:51 PM   #3
Senior Member
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian
Posts: 2,208

Rep: Reputation: 341Reputation: 341Reputation: 341Reputation: 341
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 03:55 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 11:19 AM
sudo does not work powah Linux - Security 1 12-06-2005 05:49 PM
sudo doesn't work oomfoofoo Debian 3 08-13-2005 08:34 AM
sudo doesn't work mla Linux - Software 1 10-01-2003 09:24 PM
sudo doesn't work mla Linux - Software 4 10-01-2003 08:34 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:43 AM.

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