Trying to stop a service with cron
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'
Luke Stapley
Updated on September 18, 2022Comments
-
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 almost 13 yearsHow do you exactly setup the cron job?
-
nilsonneto almost 13 yearsdid you try /usr/sbin/service powernap stop ? i.e. the full path to service
-
-
enzotib almost 13 yearsWithout spaces around
=
, of course -
Christopher B. Adkins almost 13 yearsIn crontab it should work with the spaces (unlike in a sh compatibel shell)
-
Christopher B. Adkins almost 13 years
/usr/sbin/service
is a shell script that is trying to runstop
(which issbin/stop
) here. -
Andrew Gunnerson almost 13 years@Florian: I didn't know that. Thanks for the information.
-
Luke Stapley almost 13 yearsthanks, I'm going to try both these sugestions, even though I have no idea what sh -c is. What does it do?
-
Luke Stapley almost 13 yearsThanks Florian, this worked (and I did include the spaces between the =)
-
Luke Stapley almost 13 yearsThis 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 over 12 yearsHad 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 over 11 yearsFor 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