【发布时间】: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