Shell脚本不从cron作业执行

les*_*syk 0 linux shell cron jobs

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 
Run Code Online (Sandbox Code Playgroud)

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

命令:

bash /etc/mycron/checkServices.sh
Run Code Online (Sandbox Code Playgroud)

试着嘘,只是 /etc/mycron/checkServices.sh

不运行

Dip*_*tch 5

#!/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;
Run Code Online (Sandbox Code Playgroud)

在这里工作正常...也许你想添加之后 #!/bin/sh PATH="/bin:/sbin:/usr/bin:/usr/sbin:/opt/usr/bin:/opt/usr/sbin:/usr/local/bin:/usr/local/sbin"

您也chmod 775 /etc/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到最终执行脚本.

由于bash/sh中的for循环使用IFS变量($IFS)作为分隔符,因此您也可以将该行services=(httpd named proftpd mysqld dovecot postfix webmin)设为as,services="httpd named proftpd mysqld dovecot postfix webmin"因为这更通用