3

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?

2
  • How do you exactly setup the cron job?
    – enzotib
    Jul 5, 2011 at 20:18
  • did you try /usr/sbin/service powernap stop ? i.e. the full path to service
    – fossfreedom
    Jul 5, 2011 at 20:19

3 Answers 3

4

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

PATH = /sbin:$PATH

to the crontab file.

5
  • Without spaces around =, of course
    – enzotib
    Jul 5, 2011 at 21:02
  • In crontab it should work with the spaces (unlike in a sh compatibel shell) Jul 5, 2011 at 21:19
  • Thanks Florian, this worked (and I did include the spaces between the =) Jul 9, 2011 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, 2011 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, 2012 at 13:37
2

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

Try making cron run this instead:

sh -c '/usr/sbin/service powernap stop'
4
  • /usr/sbin/service is a shell script that is trying to run stop (which is sbin/stop) here. Jul 5, 2011 at 21:21
  • @Florian: I didn't know that. Thanks for the information. Jul 6, 2011 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? Jul 7, 2011 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. Jul 9, 2011 at 7:03
2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .