server_name :服务名,ps -ef里面查看的服务名
max_num:检测多少次失败后从启
logfile:日志存放文件
默认是检测失败了3次就会
#!/bin/bash server_name=nginx max_num=3 logfile=/tmp/${server_name}_check.log function check(){ pid_number=`ps -ef |grep ${server_name}|grep -v grep|wc -l` if [ $pid_number -eq 0 ];then return 1 else return 0 fi } function start_server(){ systemctl start nginx } for num in `seq ${max_num}`;do if check;then echo "`date` ${server_name} is ok">>${logfile} exit 255 else echo "`date` ${server_name} is error">>${logfile} if [ ${num} -eq 3 ];then start_server echo "`date` ${server_name} is start">>${logfile} fi sleep 3 fi done ~运行日志截图

评论区