您的位置:首页 > 运维架构 > Shell

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: