如下shell脚本,监控liunx服务进程被杀死后,自动恢复需要多长时间
application_name="$1" #对应服务名称
echo "$application_name"
wait_time=240 #设置循环检测时间(ms)
pid=`ps -ef | grep $application_name | grep -v "auto" | grep -v "grep" | awk '{print $2}'`
if [ -n "$pid" ]; then
echo "process id:$pid "
kill -9 $pid
else
echo "process $application_name not exit"
fi
current=`date "+%Y-%m-%d %H:%M:%S"`
current_time=`date -d "$current" +%s`
sleep 1
i=1
while [[ $i -lt $wait_time ]]
do
pid2=`ps -ef | grep $application_name | grep -v "auto" | grep -v "grep" | awk '{print $2}'`
if [ ! -n "$pid2" ];then
# i=`expr $i + 1`
end=`date "+%Y-%m-%d %H:%M:%S"`
end_time=`date -d "$end" +%s`
i=`expr $end_time - $current_time `
else
echo "process is exit"
break
fi
done
end=`date "+%Y-%m-%d %H:%M:%S"`
end_time=`date -d "$end" +%s`
time=`expr $end_time - $current_time `
#recovery_time 恢复时长(ms)
echo "recovery_time:"$time
#start_time 开始时间(ms)
echo "start_time:"$current
#end_time 开始时间(ms)
echo "end_time:"$end