Shell脚本之监视主进程的运行状态,异常退出后自动重启
2015-10-29 18:36
856 查看
一、成品经过长期的自动化测试后,发现偶然出现主进程异常退出的现象,于是受开发者的委托,用SHELL写一段监视程序,要求如下:
主进程退出后,自动重启这个主进程。
主进程程序状态为Z时,重启该主进程。
主进程的CPU负载超过95%时,3分钟内持续在高位运行时,重启应用程序。
直接上代码。
二、自启动的Shell代码
主进程退出后,自动重启这个主进程。
主进程程序状态为Z时,重启该主进程。
主进程的CPU负载超过95%时,3分钟内持续在高位运行时,重启应用程序。
直接上代码。
#!/bin/sh ################################## #Author : 154353294@qq.com #Date : 2015-10-1 #Funtion: Monitor MainProcess's status, # IF the status of MainProcess process is Z ,then reboot system; # IF the MainProcess process exit abnormally,then reboot application; # IF the CPU usage of MainProcess process above 95% duration 3mins, then reboot applicaion. # The log of shell output to current directory named HealthMonitor_YYYYMMDDHHMMSS.log. ################################## var=$0 LOGFILE=${var%%.sh}_`date "+%Y%m%d%H%M%S"`.log echo "`date "+%Y-%m-%d %H:%M:%S"`"#"Start " > $LOGFILE sec=3 t_start=0 t_perCurrent=0 t_current=0 total=0 MaxTimes=60 ##3*60=180s #获取主进程的PID,内存使用率,CPU使用率,以及名称。 eval $(top -n 1| grep "MainProcess" | grep -v grep | awk {'printf("mainPID=%s;myStatus=%s;memInfo=%s;cpuRatio=%s;pName=%s\n",$1,$4,$5,$7,$8)'}) echo `date "+%Y-%m-%d %H:%M:%S"`"#"$mainPID $myStatus ${memInfo%%m} ${cpuRatio%%%} $pName >> $LOGFILE while [ true ] do while [ -n "$pName" ] ##MainProcess exist do # echo "`date "+%Y-%m-%d %H:%M:%S"` ####" >> $LOGFILE sleep $sec ####You must initialize them again!!!!! mainPID="" myStatus="" memInfo="" cpuRatio="" pName="" eval $(top -n 1| grep "MainProcess" | grep -v grep | awk {'printf("mainPID=%s;myStatus=%s;memInfo=%s;cpuRatio=%s;pName=%s\n",$1,$4,$5,$7,$8)'}) if [ ${cpuRatio%%%} -gt 95 ]; then #3分钟内持续负载都在95%以上的处理 # echo `date "+%Y-%m-%d %H:%M:%S"`"#"Cpu Usage High: $mainPID $myStatus ${memInfo%%m} ${cpuRatio%%%} $pName >> $LOGFILE if [ $total -eq 0 ]; then t_start=$(date +%s) ### set first time to Seconds total=$(($total+1)) t_perCurrent=$t_start else t_current=$(date +%s) ### set current time to Seconds if [ $total -ge $MaxTimes ]; then ## 3 Mins break; fi if [ $(($t_current-$t_perCurrent)) -ge 4 ]; then ##not continue t_start=$(date +%s) ### set current time to Seconds total=1 t_perCurrent=$t_start else total=$(($total+1)) t_perCurrent=$(date +%s) ### set current time to Seconds fi fi # else # echo `date "+%Y-%m-%d %H:%M:%S"`"#"Cpu Usage Low : $mainPID $myStatus ${memInfo%%m} ${cpuRatio%%%} $pName >> $LOGFILE fi if [ -z "$pName" ]; then break fi if [ "$myStatus" = "Z" ]; then break fi done if [ $total -ge $MaxTimes ]; then echo "`date "+%Y-%m-%d %H:%M:%S"`#Cpu usage run high duration 3 Mins!~" >> $LOGFILE echo "`date "+%Y-%m-%d %H:%M:%S"`#System will reboot application~" >> $LOGFILE killall SubProcess killall MainProcess #重启自启动的Shell /etc/init.d/mystart.sh monitor t_start=0 t_perCurrent=0 t_current=0 total=0 mainPID="" myStatus="" memInfo="" cpuRatio="" pName="" eval $(top -n 1| grep "MainProcess" | grep -v grep | awk {'printf("mainPID=%s;myStatus=%s;memInfo=%s;cpuRatio=%s;pName=%s\n",$1,$4,$5,$7,$8)'}) echo `date "+%Y-%m-%d %H:%M:%S"`"#"$mainPID $myStatus ${memInfo%%m} ${cpuRatio%%%} $pName sleep 30 continue fi if [ -z "$pName" ]; then echo "`date "+%Y-%m-%d %H:%M:%S"`#MainProcess is not exist!!" >> $LOGFILE echo "`date "+%Y-%m-%d %H:%M:%S"`#System will reboot application~" >> $LOGFILE killall SubProcess /etc/init.d/mystart.sh monitor mainPID="" myStatus="" memInfo="" cpuRatio="" pName="" eval $(top -n 1| grep "MainProcess" | grep -v grep | awk {'printf("mainPID=%s;myStatus=%s;memInfo=%s;cpuRatio=%s;pName=%s\n",$1,$4,$5,$7,$8)'}) echo `date "+%Y-%m-%d %H:%M:%S"`"#"$mainPID $myStatus ${memInfo%%m} ${cpuRatio%%%} $pName sleep 30 continue fi if [ "$myStatus" = "Z" ]; then echo "`date "+%Y-%m-%d %H:%M:%S"`#MainProcess is Z status!!" >> $LOGFILE echo "`date "+%Y-%m-%d %H:%M:%S"`#System will reboot application~" >> $LOGFILE killall SubProcess killall MainProcess reboot fi done echo "`date "+%Y-%m-%d %H:%M:%S"`"#"End ">> $LOGFILE
二、自启动的Shell代码
#!/bin/sh /home/root/SubProcess & /home/root/MainProcess -qws & if [ $1 != "Monitor" ]; then sleep 30 /home/root/HealthMonitor.sh & fi
相关文章推荐
- shell脚本:打印文本中字符数不大于6的单词
- shell脚本:打印文本中字符数不大于6的单词
- 《Linux Shell》笔记之sed与正则表达式(实例总结) 未完待续
- 一定要记住的一些shell命令
- linux shell 基础 使用日志与心得
- 安装ssh命令
- shell 字符串(六)
- shellinabox终端模拟器安装
- 终端模拟软件xshell设置编码方式UTF-8
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- Shell简易程序判断某个日期是星期几
- HBase 常用Shell命令
- shell 脚本完成对日志文件的提取之sed 学习
- 基于MDK平台 cortex-M3 shell 的实现
- bash 快捷键
- Shell编程
- LinuxShell算术运算
- linux shell中28个特殊字符的作用
- Shell统计访问ip UV、PV
- linux bash shell 流程控制(if/else )