我想设置一个脚本,它将连接到Linux服务器,然后停止/启动服务。但是在停止和启动服务之间,它应该等待~10秒并检查服务是否真的停止。
service httpd stop
--wait 10 seconds. check ps -ef | grep httpd, kill if any hanged processes
service httpd start
有人可以帮我解决一下这个问题吗?
答案 0 :(得分:1)
wait
命令用于其他目的,我们需要使用sleep
命令。
检查此链接是否存在详细差异。 https://stackoverflow.com/a/13296927/3086531
检查进程是否正在运行的脚本
#!/bin/bash
UP=$(pgrep httpd | wc -l);
if [ "$UP" -ne 1 ];
then
echo "httpd is down.";
else
echo "All is well.";
fi
您的最终代码将是
#!/bin/bash
service httpd stop
sleep 10
UP=$(pgrep httpd | wc -l);
if [ "$UP" -ne 1 ];
then
service httpd start #start serivce again, since no process are found
else
echo "Service is not stopped yet.";
killall -15 httpd #Kills all processes related to httpd
service httpd start #start httpd process
fi