系统日志监控脚本,程序关闭和启动功能
最近由于工作原因写了一个关于系统日志监控脚本,监控到某些信息后,脚本中进行程序关闭和启动的功能。
详细内容如下:
#!/bin/sh
#current date time
DATE=`date "+%Y%m%d"`
#log file path and name
LOGFILE="log/porgram.log.$DATE"
#program name
PROGRAM="porgram"
#program start command
RESTART_PROGRAM="./porgram -d"
#search string in log
FIND_STRING="malloc ost for main hash failed"
#find string from logfile
grep "$FIND_STRING" $LOGFILE >> grepinfo
if test $? -eq 0
then
#found string
echo "found"
#kill program
PROCESSID=`ps -ef|grep "$PROGRAM"|grep -v grep|grep -v PPID|awk ‘{ print $2}‘`
echo "Kill the $PROGRAM:$PROCESSID program."
kill -9 $PROCESSID
#mv logfile
DATETIME=`date "+%H:%M:%S"`
echo "Mv $LOGFILE $LOGFILE.bak$DATETIME"
mv $LOGFILE $LOGFILE.bak`date "+%H:%M:%S"`
#restart program
while true
do
$RESTART_PROGRAM
NEWPROCESSID=`ps -ef|grep "$PROGRAM"|grep -v grep|grep -v PPID|awk ‘{ print $2}‘`
if test $PROCESSID -gt 0
then
echo "$PROGRAM started"
break
else
echo "try start $PROGRAM again"
fi
done
else
#not found
echo "not found"
fi