Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

On my Ubuntu 11.04 server when I try to run

 service powernap stop

as a cron job with the root user, it fails and gives me this message in syslog

 NAS CRON[10853]: (CRON) error (grandchild #10854 failed with exit status 2)

and this is emailed to the root users email account

 exec: 129: stop: not found

I can start and stop the powernap service from the command line so why doesn't it work from cron?

share|improve this question
    
How do you exactly setup the cron job? –  enzotib Jul 5 '11 at 20:18
    
did you try /usr/sbin/service powernap stop ? i.e. the full path to service –  fossfreedom Jul 5 '11 at 20:19
add comment

3 Answers

up vote 4 down vote accepted

Looks like /sbin is not in $PATH when starting it from cron. Add a line like

PATH = /sbin:$PATH

to the crontab file.

share|improve this answer
    
Without spaces around =, of course –  enzotib Jul 5 '11 at 21:02
    
In crontab it should work with the spaces (unlike in a sh compatibel shell) –  Florian Diesch Jul 5 '11 at 21:19
    
Thanks Florian, this worked (and I did include the spaces between the =) –  Luke Stapley Jul 9 '11 at 7:01
    
Had the same issue. Cron was setting PATH to /usr/bin:/bin. Adding /sbin and /usr/sbin to the path in my script fixed it right up. –  m0j0 Dec 21 '11 at 15:34
    
For some reason this did not work for me. I still have to investigate why. $PATH was not evaluated in the expression. This worked for me: PATH=/usr/sbin:/usr/bin:/bin –  rosch Aug 17 '12 at 13:37
add comment

I've had exactly this issue, and it would appear that the $PATH variable is completely empty when crontab is run, so it's not enough to put PATH=/usr/sbin;$PATH at the top of the crontab list.

So, what I did was (since I'm running this crontab as root, so I can turn off the squid proxy):

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin

and it seems to have worked.

share|improve this answer
add comment

Hmmm...it's strange that cron is trying to run "stop"...

Try making cron run this instead:

sh -c '/usr/sbin/service powernap stop'
share|improve this answer
    
/usr/sbin/service is a shell script that is trying to run stop (which is sbin/stop) here. –  Florian Diesch Jul 5 '11 at 21:21
    
@Florian: I didn't know that. Thanks for the information. –  Xiao-Long Chen Jul 6 '11 at 5:04
    
thanks, I'm going to try both these sugestions, even though I have no idea what sh -c is. What does it do? –  Luke Stapley Jul 7 '11 at 18:30
    
This just gave me a different error message CRON[11394]: (CRON) error (grandchild #11395 failed with exit status 127), but thanks for your help anyway. –  Luke Stapley Jul 9 '11 at 7:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.