Shell脚本不从cron作业执行 [英] Shell script doesn't execute from cron job

查看:207
本文介绍了Shell脚本不从cron作业执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

shell脚本:

#!/bin/sh
services=( httpd named proftpd mysqld dovecot postfix webmin)

for service in ${services[@]}
do

if ps ax | grep -v grep | grep $service > /dev/null
then
    echo "$service service running, everything is fine"     
else
    echo "$service is not running"
    service $service start
fi

done 

文件可执行文件,从root用户运行

file executable, running from root user

命令:

bash /etc/mycron/checkServices.sh

尝试了sh和 /etc/mycron/checkServices.sh

不会运行

推荐答案

#!/bin/sh
services=( httpd named proftpd mysqld dovecot postfix webmin)

for service in ${services[@]}; do
    if ps ax | grep -v grep | grep $service > /dev/null; then
        echo "$service service running, everything is fine";
    else
        echo "$service is not running";
        service $service start;
    fi;
done;

在这里工作...也许你想在#! bin / sh PATH =/ bin:/ sbin:/ usr / bin:/ usr / sbin:/ opt / usr / bin:/ opt / usr / sbin: usr / local / bin:/ usr / local / sbin

Works fine here... maybe you want to add after #!/bin/sh PATH="/bin:/sbin:/usr/bin:/usr/sbin:/opt/usr/bin:/opt/usr/sbin:/usr/local/bin:/usr/local/sbin"

您也可以使用 chmod 775 / mycron / checkServices.sh 以使其可执行,这是cron所需要的。然后你也不需要调用 bash /etc/mycron/checkServices.sh ,只需调用 /etc/mycron/checkServices.sh #!/ bin / sh 告诉可执行加载器加载 / bin / sh 如果你调用 bash /etc/mycron/checkServices.sh ,你将开始bash,然后他会开始 / bin / sh

You could also do chmod 775 /etc/mycron/checkServices.sh to make it executable, which is needed for cron. Then you would also not need to call bash /etc/mycron/checkServices.sh and can just call /etc/mycron/checkServices.sh the #!/bin/sh tells the executable loader to load the file with /bin/sh if you invoke bash /etc/mycron/checkServices.sh you will start bash which on his turn would start /bin/sh to finally execute your script.

由于bash / sh中的for循环使用IFS变量( $ IFS )作为分隔符,您还可以使 services =(httpd命名为proftpd mysqld dovecot postfix webmin) services =httpd named proftpd mysqld dovecot postfix webmin,因为这是更一般的

Since the for loop in bash / sh uses the IFS variable ($IFS) as delimiter, you could also make the line services=(httpd named proftpd mysqld dovecot postfix webmin) as services="httpd named proftpd mysqld dovecot postfix webmin" since this is more general

这篇关于Shell脚本不从cron作业执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆