简体   繁体   English

Shell 脚本不会从 cron 作业执行

[英]Shell script doesn't execute from cron job

shell script: 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 

file executable, running from root user文件可执行文件,从 root 用户运行

command:命令:

bash /etc/mycron/checkServices.sh

tried sh and just /etc/mycron/checkServices.sh尝试了 sh 并且只是/etc/mycron/checkServices.sh

doesn't run不运行

#!/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;

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"在这里工作正常......也许你想在#!/bin/sh PATH="/bin:/sbin:/usr/bin:/usr/sbin:/opt/usr/bin:/opt/usr/sbin:/usr/local/bin:/usr/local/sbin"之后添加PATH="/bin:/sbin:/usr/bin:/usr/sbin:/opt/usr/bin:/opt/usr/sbin:/usr/local/bin:/usr/local/sbin"

You could also do chmod 775 /etc/mycron/checkServices.sh to make it executable, which is needed for cron.您还可以执行chmod 775 /etc/mycron/checkServices.sh以使其可执行,这是 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 /etc/mycron/checkServices.sh并且可以调用/etc/mycron/checkServices.sh #!/bin/sh告诉可执行加载器使用/bin/sh加载文件,如果您调用bash /etc/mycron/checkServices.sh您将启动 bash ,轮到他将启动/bin/sh以最终执行您的脚本。

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由于 bash / sh 中的 for 循环使用 IFS 变量 ( $IFS ) 作为分隔符,因此您还可以将行services=(httpd named proftpd mysqld dovecot postfix webmin) as services="httpd named proftpd mysqld dovecot postfix webmin"因为这个更一般

Just as a general diagnostic process, it's sensible to insert trace statements into the script such as:就像一般诊断过程一样,将跟踪语句插入脚本是明智的,例如:

  • echo "Starting..." echo "开始..."
  • echo "Checking for running service '$service'..." echo "正在检查正在运行的服务 '$service'..."
  • which/whereis service which/whereis 服务
  • echo "Service has been started." echo "服务已启动。"
  • temporarily remove the > /dev/null for ps .暂时删除ps> /dev/null

Then execute under cron with stdout and stderr redirected to a log file.然后在 cron 下执行,并将 stdout 和 stderr 重定向到日志文件。

That allows you to identify the exact line that's failing.这使您可以确定发生故障的确切线路。 As others have said, it looks likely to be the "service" or "$service" commands aren't found because the PATH isn't set to include them.正如其他人所说,看起来可能是找不到“服务”或“$service”命令,因为 PATH 未设置为包含它们。 You do realise "service" itself is being sought as an external command that presumably launched $service in turn?您确实意识到“服务”本身正在被寻求作为可能依次启动 $service 的外部命令? Also keep an eye on root's mail, as cron sometimes sends error reports via mail.还要留意 root 的邮件,因为 cron 有时会通过邮件发送错误报告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM