shell脚本不从cron作业执行

问题描述:

shell脚本:shell脚本不从cron作业执行

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

不运行

+0

你有什么错误? – 2011-06-14 00:25:07

+0

您是否为cron工作设置了计划?发布完整的crontab通信 – ngen 2011-06-14 00:25:48

+1

制作第二行“printenv>/tmp/crontest”并告诉我们该文件的UID以及您的PATH是什么。 – 2011-06-14 00:30:09

#!/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/shPATH="/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使用IFS变量($IFS)作为分隔符,你也可以使线services=(httpd named proftpd mysqld dovecot postfix webmin)作为services="httpd named proftpd mysqld dovecot postfix webmin",因为这是更普遍的

+0

@lesyk为了回应你的编辑建议:请在这里留言,并且说**更多**而不是“不起作用”。 – phihag 2011-06-19 11:32:37

就像一般的诊断过程,这是明智的插入跟踪语句到脚本如:

  • 回声“开始......”
  • 回声“检查运行服务‘$服务’......”
  • 其中/ wherei s服务
  • echo“服务已启动。”
  • 暂时删除> /dev/nullps

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

这可以让你找出失败的确切路线。正如其他人所说,看起来可能是“服务”或“$ service”命令未找到,因为PATH没有设置为包含它们。你确实意识到“服务”本身正在被寻求作为一个外部命令,大概推出$服务?还要留意root的邮件,因为cron有时会通过邮件发送错误报告。