简体   繁体   中英

run sh script to stop and restart nginx as root from crontab?

I need a script to run a bash script that stops nginx dows a bunch of other stuff and then starts nginx

the crontab looks like this

5 10 * * 0 sh ~/runnow/script.sh

and I edited the crontab well logged in as root.

the script runs fine however the lines for stop and start of nginx give the error service not found.

echo "Stopping Nginx"
service nginx-sp stop

gives

Stopping Nginx
/root/run/script.sh: 2: /root/run/script.sh: service: not found

if I manually run the script logged on as root it runs fine. my thought is that crontab is not being run as root but searching for that it seems if you exited your crontab when logged in as root with crontab -e it should be run as root. and it must be since I am using ~/run/script.sh to run the script and the real path is /root/run/script.sh

so... im lost....

As cyrus pointed ... use which to find service path in this way:

which service

Then add full path in your script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM