Shell写的一个进程监视脚本
2010-06-23 12:56
501 查看
我3年前写的的一个程序。 整理文档时发现的。
用于监视一个进程的活动,记录它的内存,当它崩溃时往指定邮箱发信,并且自动重启该程序。
用于监视一个进程的活动,记录它的内存,当它崩溃时往指定邮箱发信,并且自动重启该程序。
#!/bin/bash ############## # USER INI # ############################################################ #Mail sender address mailfrom="test@from.com" #Mail list. Use "," to delimit every mail address. mailto="test@to.com" #The interval of memroy recording interval=3#The basic file name of recording memory fileBaseName="WatchProcessLog" ############### # MAIN SCRIPT # ############################################################ #set system signal trap OnInterrupt(){ kill $chpid echo "" echo "" echo "Received signal INTERRUPT!" echo "Kill process $PROGRAM(PID:$chpid)" echo "" exit } SendBeginMail(){ /usr/sbin/sendmail -t <<EOF From: $mailfrom To: $mailto Subject: [No-reply]WatchProcess begin to watch Hello $mailto Now I am glad to inform you, that WatchProcess begin to watch process $PROGRAM(PID:$chpid) on $hostname . Begin Report: Begin time : $beginTime Process name : $PROGRAM Process PID : $chpid Memory info. : $MEMINFO ----------------------------------------------------- *This is a mail from Program WatchProcess *Do not reply this mail. *At the 3 conditions below, you will receive a mail: * 1, This program begin; * 2, The process $PROGRAM is collapsed; * 3, This program end. ------------------------------------------------------ EOF } SendCollapseMail(){ } #read program name echo "Input the program name you want to run and watch:" while [ 1 ] do read PROGRAM echo "" if [ -e $PROGRAM ]; then if [ -x $PROGRAM ]; then break else echo "You don't have right to run $PROGRAM." echo "Please check your right!" exit fi else echo "File '$PROGRAM' is not exist!" echo "Maybe you forget the path of '$PROGRAM'." echo "" echo "Input program name with path:" fi done #read option and try to run program while [ 1 ] do echo "Input run options:" read OPTION echo "" if ./$PROGRAM $OPTION & then sleep 1 chpid="$!"; counter=`ps -u $USER | grep $PROGRAM | grep $chpid | grep -v grep | wc -l` if [ $counter -eq 0 ] then echo "Can't run '$PROGRAM' with option '$OPTION'." echo "" else echo "$PROGRAM has been run successfully." echo "${PROGRAM}'s PID is $chpid ." echo "" break fi else echo "Status : $?" echo "Run error!!!!!!1" fi done trap OnInterrupt INT #make a file name with the basename and current time fileName=${fileBaseName}`date +"%Y%m%d-%H%M%S"`.log if [ -e $fileName ]; then echo "Error!!!" echo "There is a same file named '$fileName' exist!!!" echo "Try to run once again!" echo "" exit else echo "Create log file : $fileName" echo "" fi #watch program beginTime=`date +"%Y-%m-%d %H:%M:%S"` echo "Memory record begin at $beginTime ." echo "Use CTRL+C to exit ..." echo "Memory record begin at $beginTime" >> $fileName echo "VSZ RSS" >> $fileName echo `top -b -n 1 | grep $USER | grep $PROGRAM | awk '{ print $5" "$6 }'` >> $fileName MEMINFO=`top -b -n 1 | grep $USER | grep $PROGRAM | awk '{ print "VSZ:"$5" RSS:"$6 }'` SendBeginMail while [ 1 ] do sleep $interval counter=`ps -u $USER | grep $PROGRAM | grep $chpid | grep -v grep | wc -l` if [ $counter -eq 0 ]; then currentTime=`date +"%Y-%m-%d %H:%M:%S"` echo "Process is collapsed at $currentTime" >> $fileName echo "Process $PROGRAM(PID:$chpid) is not exist any more." echo "Will restart $PROGRAM in 5 secs." echo "" sleep 5 ./$PROGRAM $OPTION & chpid="$!"; currentTime=`date +"%Y-%m-%d %H:%M:%S"` echo "Memory record restart at $currentTime" echo "$PROGRAM has been restarted successfully." echo "${PROGRAM}'s new PID is $chpid ." echo "" else echo `top -b -n 1 | grep $USER | grep $PROGRAM | awk '{ print $5" "$6 }'` >> $fileName fi done
相关文章推荐
- linux下监视进程 崩溃挂掉后自动重启的shell脚本
- Shell脚本实现监视指定进程的运行状态
- Shell脚本之监视指定进程的执行状态
- linux下如何自动检测并重新启动一个死掉的进程(shell脚本实现)
- linux下监视进程 崩溃挂掉后自动重启的shell脚本
- 一个监控Squid运行进程数并自动重启的简洁Shell脚本分享
- 如何编写一个shell脚本查看某个进程是否在运行
- shell脚本中关于进程替换的一个应用
- Shell脚本之监视指定进程的运行状态
- 一个监控Squid运行进程数并自动重启的简洁Shell脚本分享
- Shell脚本实现监视指定进程的运行状态
- Shell脚本之监视主进程的运行状态,异常退出后自动重启
- linux下如何自动检测并重新启动一个死掉的进程,然后再把它杀死:)(shell脚本实现)
- 写一个 shell 脚本 守候rocketmq 进程不当掉
- 通过脚本的方式杀tomcat进程(shell脚本),shell中将一个文件的内容读出赋值给一个变量
- 自己编写的一个查找Linux进程的Shell脚本
- linux下监视进程 崩溃挂掉后自动重启的shell脚本
- Shell脚本编程——监视内存占用最高的进程
- 【原】用shell脚本监视进程,崩溃挂掉后自动重启
- shell脚本是否作为单独的一个进程执行?