进程监控---通用
程序员文章站
2022-06-04 19:21:37
...
#! /bin/sh
# supervisor process
LOG_PWD=/var/log/dcr_process_sh
if [[ ! -d $LOG_PWD ]]; then
/bin/mkdir -p $LOG_PWD
fi
DAY=$(date +%Y-%m-%d)
DAY_7=$(date "+%Y%m%d" -d "-7 day")
LOG_FILE=$LOG_PWD/dcr_process_$DAY.log
p_name="haproxy"
num=0
# log function
function log() {
local t=$(date +"%Y-%m-%d %H:%M:%S")
echo "[ $t ] $0 : $1 " >> ${LOG_FILE}
}
# check process number
# $1 : process name
function check_process() {
if [ -z $1 ]; then
log "Input parameter is empty."
return 0
fi
p_num=$(ps -e | grep "$1" | grep -v "grep" | wc -l)
log "p_num = $p_num"
echo $p_num
}
#msalarm $1 (alarm_parameter)
function MsAlarm()
{
msalarm -n DCR_process_error -p $1
log "/usr/local/bin/wsms_areport -n DCR_process_error -p $1"
}
function log_del()
{
#每天8:00删除7天前的脚本执行结果日志
deltime=$(date "+%H%M")
if [ "$deltime" == "0800" ]; then
#删除7天前的文件
dellog_file=$LOG_PWD/dcr_process_$DAY_7.log
if [ -f "$dellog_file" ];then
rm -f ${dellog_file}
fi
fi
}
# supervisor process
main(){
declare -i ch_num
ch_num=$(check_process $p_name)
if [ $ch_num -eq $num ]; then
killall $p_name 2>&1 >/dev/null
service $p_name start 2>&1 >/dev/null
MsAlarm $p_name
log "$p_name start"
else
log "$p_name is normal"
fi
log_del
}
main
上一篇: HashMap