1.利用shell监控某一进程,并在进程挂掉的时候自动重启
#!/bin/bash
ps -ef | grep "zyfw-cps" | grep -v "grep" #检测进程是否存在
if [ ! "$?" -eq 0 ];then #判断上条命令的执行状态,若检测到存在,会返回0,若检测在不存在,会返回1.
pid=`ps -ef | grep zyfw-cps | grep -v "grep" | awk '{print $2}'`
kill -9 $pid && echo "`date "+%Y-%m-%d %H:%M:%S"` kill $pid" >> /mnt/zyfw-cps/restart.logs
cd /mnt/zyfw-cps && ./startup.sh #执行预先写好的启动脚本
echo "`date "+%Y-%m-%d %H:%M:%S"` zyfw-cps have been restart!!!!" >>/mnt/zyfw-cps/restart.logs #将重启信息写进重启日志里,方便日后故障排查。
fi
#!/bin/bash
code=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://10.172.13.26:8080/zyfw-cps/admin/index.jsp`
if [ ! "$code" -eq 200 ];then
pid=`ps -ef | grep zyfw-cps | grep -v "grep" | awk '{print $2}'`
kill -9 $pid && echo "`date "+%Y-%m-%d %H:%M:%S"` kill $pid" >> /mnt/zyfw-cps/restart.logs
cd /mnt/zyfw-cps && ./startup.sh
echo "`date "+%Y-%m-%d %H:%M:%S"` zyfw-cps have been restart!!!!" >> /mnt/zyfw-cps/restart.logs
fi