Shell 脚本不从 cron 作业执行

发布于 2024-11-15 01:28:38 字数 520 浏览 3 评论 0原文

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 用户运行

命令:

bash /etc/mycron/checkServices.sh

尝试 sh 并且只是 /etc/mycron/checkServices.sh

不运行

shell script:

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

command:

bash /etc/mycron/checkServices.sh

tried sh and just /etc/mycron/checkServices.sh

doesn't run

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖阳 2024-11-22 01:28:38
#!/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"

你也可以这样做 chmod 775 /etc/mycron/checkServices.sh使其可执行,这是需要的计划任务。那么您也不需要调用 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)作为 services="httpd named proftpd mysqld dovecot postfix webmin" 因为这更通用

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

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.

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

你怎么敢 2024-11-22 01:28:38

正如一般的诊断过程一样,明智的做法是在脚本中插入跟踪语句,例如:

  • echo "Starting..."
  • echo "Checking for running service '$service'..."
  • which/whereis service
  • echo "Service has beenstarted ”。
  • 暂时删除 > /dev/null 代表 ps

然后在 cron 下执行,并将 stdout 和 stderr 重定向到日志文件。

这使您可以确定出现故障的确切线路。正如其他人所说,看起来可能是找不到“service”或“$service”命令,因为 PATH 未设置为包含它们。您确实意识到“服务”本身正在被视为可能依次启动 $service 的外部命令?还要留意 root 的邮件,因为 cron 有时会通过邮件发送错误报告。

Just as a general diagnostic process, it's sensible to insert trace statements into the script such as:

  • echo "Starting..."
  • echo "Checking for running service '$service'..."
  • which/whereis service
  • echo "Service has been started."
  • temporarily remove the > /dev/null for ps.

Then execute under cron with stdout and stderr redirected to a log file.

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. You do realise "service" itself is being sought as an external command that presumably launched $service in turn? Also keep an eye on root's mail, as cron sometimes sends error reports via mail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文