【问题标题】:Start nginx from shell script on crontab从 crontab 上的 shell 脚本启动 nginx
【发布时间】:2019-10-18 04:43:44
【问题描述】:

我想每 1 分钟检查一次 NGINX 是否正在运行。 我的 shell 脚本是:

#!/bin/sh
ps auxw | grep nginx | grep -v grep > /dev/null
if [ $? != 0 ]
then
echo "NGINX is not running"
/etc/init.d/nginx start
else
echo "NGINX is running"
fi

使用sh launch.sh 正确运行脚本(如果 NGINX 没有运行,请运行 NGINX)。 问题是当我想通过 crontab 每 1 分钟运行一次脚本时,什么也没有发生。 crontab 列表在这里:

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

* * * * * ~/sh launch.sh

我测试了* * * * * sh launch.sh* * * * * launch.sh* * * * * ./launch.sh,但它们都不能正常工作。 我的操作系统是 UBUNTU 18.04。

这是日志:

Jun  3 08:28:01 hajitsu-VirtualBox CRON[3239]: (root) CMD (~/launch.sh)
Jun  3 08:28:01 hajitsu-VirtualBox CRON[3240]: (hajitsu) CMD (/home/hajitsu/launch.sh)
Jun  3 08:28:01 hajitsu-VirtualBox CRON[3238]: (CRON) info (No MTA installed, discarding output)
Jun  3 08:28:01 hajitsu-VirtualBox CRON[3237]: (CRON) info (No MTA installed, discarding output)
Jun  3 08:29:01 hajitsu-VirtualBox CRON[3374]: (root) CMD (~/launch.sh)
Jun  3 08:29:01 hajitsu-VirtualBox CRON[3373]: (CRON) info (No MTA installed, discarding output)
Jun  3 08:29:01 hajitsu-VirtualBox CRON[3376]: (hajitsu) CMD (/home/hajitsu/launch.sh)
Jun  3 08:29:01 hajitsu-VirtualBox CRON[3372]: (CRON) info (No MTA installed, discarding output)

我认为命令已触发,但没有发生任何事情。

【问题讨论】:

  • 他们怎么不工作了?很难猜测到底发生了什么。
  • 另外,您是否尝试过restart loop?这是确保服务正常运行的更好方法。
  • 尝试完整路径,例如/home/user1/launch.sh 记住cron 没有环境变量,也没有您的$PATH 设置。
  • 如果您需要帮助,您需要说明它是如何不工作的。例如,您可以在 cron 日志中查找错误消息。

标签: shell ubuntu nginx service cron


【解决方案1】:

NGINX 需要 sudo 权限。

如果你有 sudo 权限,你可以修改/etc/sudoers.d/username 文件并在没有密码的情况下执行sudo 命令。

该文件通常包含用户和用户无需指定密码即可运行的命令列表。在您的情况下,您可以运行:

sudo /etc/init.d/nginx start

添加或修改您的 sudoers 文件。 (用您的用户名替换用户名。)

$ EDITOR=nano sudo visudo -f /etc/sudoers.d/username  # EDITOR=nano sets my editor (because I am more comfortable with nano)

复制并粘贴以下内容。 您可以添加更多以逗号分隔的sudo 命令。

username ALL=(ALL) NOPASSWD: /etc/init.d/nginx start,/etc/init.d/nginx start

注意:命令只会在使用sudo 调用时执行。

在您的 launch.sh 中添加 sudo

#!/bin/sh
ps auxw | grep nginx | grep -v grep > /dev/null
if [ $? != 0 ]
then
echo "NGINX is not running"
sudo /etc/init.d/nginx start
else
echo "NGINX is running"
fi

使文件可执行。

$ chmod +x launch.sh

【讨论】:

  • 感谢您的重播,在 crontab 中,我写了 * * * * * /home/hajitsu/launch.sh 并且它成功了。
【解决方案2】:

~ 在 crontab 中不会像在交互式 shell 中那样扩展。请改用/home/username

【讨论】:

  • 我的字体想用bash,就用shell
  • 这与 Bash 无关。
猜你喜欢
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多