fendou00sd 2019-12-29
shell 监控脚本,通过会while true 实现,没有使用crontab
#!/bin/bash source /etc/profile appname=aurora-gateway-provider-1.3.1_build2.jar #【jps 显示的 进程名称】 app_path=/opt/workspace/aurora-gateway/ #【app_script 的上层目录,和启动方式配合,如 aurora-gateway的启动方式是 sh sbin/app.sh start】 app_script=app.sh #【启动脚本】 # 如下内容不需要修改 function monitor() { monitorOutputLog=/data/aurora-gateway/logs/monitor/$(date ‘+%Y%m%d‘).log if [ ! -f "$monitorOutputLog" ];then touch "$monitorOutputLog" fi pid=$(jps |awk -v var=$appname ‘{if($2 ~ var)print$1}‘) #awk -v 表示将shell中的变量appname引入 awk表达式;{if($2 ~ var)print$1}判断是不是相似,相似打印pid if (kill -0 $pid 2>/dev/null);then # kill -0 $pid:检查pid进程是否存在,echo $? 存在则返回0;不存在返回1; 2>/dev/null:表示不输出错误信息 echo "$(date ‘+%Y-%m-%d %H:%M:%S‘) $appname is running" >> $monitorOutputLog sleep 15 #sleep 15秒 else cd $app_path && sh sbin/$app_script start pid=$(jps |awk -v var=$appname ‘{if($2 ~ var)print$1}‘) if (kill -0 $pid 2>/dev/null);then echo "$(date ‘+%Y-%m-%d %H:%M:%S‘) $appname 启动成功" >> $monitorOutputLog sleep 15 else echo "$(date ‘+%Y-%m-%d %H:%M:%S‘) $appname 启动失败" >> $monitorOutputLog sleep 15 fi fi } while true do monitor done # 使用步骤: # [root] mkdir -p /data/aurora-gateway/logs/monitor/ # [root] chown deploy:deploy /data/aurora-gateway/logs/monitor/ # 拷贝aurora_gateway_monitor.sh 到 /opt/workspace/aurora-gateway/ 目录下 # [root] chown deploy:deploy /opt/workspace/aurora-gateway/aurora_gateway_monitor.sh # chmod +x /opt/workspace/aurora-gateway/aurora_gateway_monitor.sh # [root] su - deploy # cd /opt/workspace/aurora-gateway/ # ./aurora_gateway_monitor.sh& # 检查日志是否正常输出