简体   繁体   English

运行sh脚本以从crontab以root身份停止并重新启动Nginx?

[英]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 我需要一个脚本来运行一个bash脚本,该脚本将停止nginx拖入一堆其他东西,然后启动nginx

the crontab looks like this crontab看起来像这样

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

and I edited the crontab well logged in as root. 我编辑了crontab,并以root身份登录。

the script runs fine however the lines for stop and start of nginx give the error service not found. 该脚本可以正常运行,但是nginx的停止和启动行提供了未找到的错误服务。

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. 如果我手动运行以root用户身份登录的脚本,则运行正常。 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. 我的想法是crontab不是以root身份运行,而是搜索它,如果您使用crontab -e以root身份登录时退出crontab,似乎它应该以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 一定是因为我使用〜/ run / script.sh来运行脚本,而实际路径是/root/run/script.sh

so... im lost.... 所以...我迷路了...

As cyrus pointed ... use which to find service path in this way: 正如cyrus所指出的...使用它以这种方式查找服务路径:

which service

Then add full path in your script. 然后在脚本中添加完整路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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