问题描述
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用户运行
file executable, running from root user
命令:
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
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"
您也可以使用 chmod 775 / 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
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.
由于bash / sh中的for循环使用IFS变量(
$ IFS
)作为分隔符,您还可以使 services =(httpd命名为proftpd mysqld dovecot postfix webmin)
为 services =httpd named proftpd mysqld dovecot postfix webmin
,因为这是更一般的
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
这篇关于Shell脚本不从cron作业执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!