从crontab上的shell脚本启动nginx

从crontab上的shell脚本启动nginx,shell,ubuntu,nginx,service,cron,Shell,Ubuntu,Nginx,Service,Cron,我想每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)。 问题是,当我

我想每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)

我认为命令启动了,但什么也没发生。

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

NGINX需要sudo权限

如果您具有sudo权限,则可以修改
/etc/sudoers.d/username
文件并执行
sudo
命令,而无需密码

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

sudo/etc/init.d/nginx start

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

复制并粘贴以下内容。 您可以添加更多用逗号分隔的
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

他们怎么不工作?很难猜测到底发生了什么。另外,你有没有尝试过一种新的方法?这是确保服务正常运行的更好方法。可能重复的完整路径,如
/home/user1/launch.sh
请记住
cron
没有环境变量,也没有您的
$path
设置。如果需要帮助,您需要指定它的工作方式。例如,您可以在cron日志中查找错误消息。我想使用bash,只需Shell。这与bash无关。感谢您的重播,在crontab中,我编写了
***/home/hajitsu/launch.sh
,它已经工作了。
$ chmod +x launch.sh