我想设置一个脚本,它将连接到linux服务器,然后停止/启动服务。但是在服务停止和启动之间,它应该等待大约10秒,并检查服务是否真的停止了。
service httpd stop
--wait 10 seconds. check ps -ef | grep httpd, kill if any hanged processes
service httpd start
有没有人能帮帮我怎么做?
发布于 2017-06-16 03:40:22
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
https://stackoverflow.com/questions/44575158
复制相似问题