强制关闭失败时强制终止
问题描述:
我们有一个bash脚本,可以关闭我们的应用程序,特别是我们有这样的停止功能。强制关闭失败时强制终止
doStop(){
pid=`cat ${pidfile}`
echo "Gracefully terminating server with pid $pid"
kill ${pid}
echo "Server stopped"
}
但是有可能杀将因各种原因失败,我们要为杀死它像这样...
kill -9 ${pid}
有没有办法等待杀,如果它工作,没有发出kill -9?
答
从man
kill`
杀 - 将信号发送到一个进程
这意味着kill
没有接收到来自所述过程的任何响应。 “等待”过程的唯一方法是检查过程是否存在一段时间,并在时间不足时发送给他。
e.g(未测试)
doStop(){
pid=$(cat ${pidfile})
echo "Gracefully terminating server with pid $pid"
kill ${pid}
let count=60
while [[ $count -ge 0 ]] && [[ -n "$(ps --pid ${pid} -o pid=)" ]]
do
sleep 1
let "count--"
done
if [[ -n "$(ps --pid ${pid} -o pid=)" ]];
then
kill -9 ${pid}
fi
if [[ -n "$(ps --pid ${pid} -o pid=)" ]];
then
echo "Server stopped"
exit 0
else
echo "Failed to stop server"
exit 1
fi
}
可能'kill'本身需要它的时候。所以在它之后,你可以写和检查它是否仍在运行,并继续执行杀死它。 – fedorqui