且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Shell脚本不从cron作业执行

更新时间:2023-12-05 07:51:10

#!/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 $ c

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