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
不运行
#!/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
以使其可执行,这是cron所需要的。 那么你也不需要调用bash /etc/mycron/checkServices.sh
,只需调用/etc/mycron/checkServices.sh
#!/bin/sh
告诉可执行文件加载器使用/bin/sh
加载文件if你调用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"
是比较一般的
就像一般的诊断过程一样,将跟踪语句插入到脚本中是明智的,例如:
ps
的> /dev/null
。 然后在cron下执行stdout和stderr重定向到一个日志文件。
这使您可以确定失败的确切路线。 正如其他人所说,它可能是“服务”或“$服务”命令没有找到,因为没有设置PATH包括它们。 你真的意识到“服务”本身正在被寻求作为一个外部的命令,大概推出$服务轮流? 还要留意root的邮件,因为cron有时会通过邮件发送错误报告。