Trying to stop a service with cron

9,174

Solution 1

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

PATH = /sbin:$PATH

to the crontab file.

Solution 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.

Solution 3

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:
9,174
Luke Stapley
Author by

Luke Stapley

Updated on September 18, 2022

Comments

  • Luke Stapley
    Luke Stapley over 1 year

    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?

    • enzotib
      enzotib almost 13 years
      How do you exactly setup the cron job?
    • nilsonneto
      nilsonneto almost 13 years
      did you try /usr/sbin/service powernap stop ? i.e. the full path to service
  • enzotib
    enzotib almost 13 years
    Without spaces around =, of course
  • Christopher B. Adkins
    Christopher B. Adkins almost 13 years
    In crontab it should work with the spaces (unlike in a sh compatibel shell)
  • Christopher B. Adkins
    Christopher B. Adkins almost 13 years
    /usr/sbin/service is a shell script that is trying to run stop (which is sbin/stop) here.
  • Andrew Gunnerson
    Andrew Gunnerson almost 13 years
    @Florian: I didn't know that. Thanks for the information.
  • Luke Stapley
    Luke Stapley almost 13 years
    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
    Luke Stapley almost 13 years
    Thanks Florian, this worked (and I did include the spaces between the =)
  • Luke Stapley
    Luke Stapley almost 13 years
    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.
  • m0j0
    m0j0 over 12 years
    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.
  • rosch
    rosch over 11 years
    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