【问题标题】:run sh script to stop and restart nginx as root from crontab?运行 sh 脚本以从 crontab 以 root 身份停止并重新启动 nginx?
【发布时间】:2017-08-10 23:16:33
【问题描述】:

我需要一个脚本来运行一个停止 nginx 的 bash 脚本,然后启动 nginx

crontab 看起来像这样

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

我编辑了以 root 身份登录的 crontab。

脚本运行良好,但是 nginx 的停止和启动行给出了错误服务未找到。

echo "Stopping Nginx"
service nginx-sp stop

给了

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

如果我手动运行以 root 身份登录的脚本,它运行良好。 我的想法是 crontab 没有以 root 身份运行,但是如果您在使用 crontab -e 以 root 身份登录时退出了您的 crontab,那么它应该以 root 身份运行。一定是因为我使用~/run/script.sh 来运行脚本,而真正的路径是/root/run/script.sh

所以……我迷路了……

【问题讨论】:

  • 我建议将service 替换为/path/to/service

标签: linux bash nginx cron


【解决方案1】:

正如赛勒斯所说... 使用 which 来查找服务路径:

which service

然后在脚本中添加完整路径。

【讨论】:

    猜你喜欢
    • 2021-02-26
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 2011-10-30
    • 2023-03-16
    • 2014-05-21
    相关资源
    最近更新 更多