Shell 脚本不从 cron 作业执行
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里工作正常...也许你想在
#!/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"
因为这更通用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 callbash /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 invokebash /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 lineservices=(httpd named proftpd mysqld dovecot postfix webmin)
asservices="httpd named proftpd mysqld dovecot postfix webmin"
since this is more general正如一般的诊断过程一样,明智的做法是在脚本中插入跟踪语句,例如:
> /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:
> /dev/null
forps
.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.