进程监控

当你在某台机器上运行着某个daemon,假设有一天它由于异常崩溃或终止,也许你不会及时知道,如果你维护着多个类似的程序,或许他们还在不同的机器上时,情况可能会变的更糟。

再或者你想让一封邮件来告知你某个program是否执行完成,而不是隔一段时间去ps下,等等...

那么下面这个小工具都可以帮你完成。

 

功能:

监控进程,指定被监控的进程一旦由于异常退出,则发邮件通知,邮件中包含一些较为重要的信息,如机器ip,命令行,另外还可以考虑显示程序执行所在的目录

 

命令:

$sh detector_pid.sh -h

usage:

sh detector_pid.sh <start|stop|status|stopall> [pid]

 

参数说明:

start pid 启动对某个进程pid的监控

stop pid 停止某个监控程序

status 显示监控列表,包含3列,分别是监控进程pid,被监控进程pid,被监控进程command

stopall 停止监控列表里的所有监控


#!/bin/bash
# author: lijingcheng3359@gmail.com

function help()
{
    echo -e 'usage:\nsh detector_pid.sh <start|stop|status|stopall> [pid]'
}
function init()
{
    if [ ! -f '.detected' ] ; then
	echo "touch .detected for record!"
	touch '.detected'
    fi
}
function start()
{
    if [ "$1" = "" ] ; then
	help
	exit 1
    fi
    ps -p "$1" >/dev/null
    if [ "$?" -eq 0 ] ; then
	echo "start watch process $1!"
	nohup sh detector_pid.sh daemon "$1" &
	echo -e "$!\t"`ps -p $1 o pid,cmd | grep -v PID` >> .detected
    else
	echo "process $1 is not exist!"
    fi
}
function stop()
{
    if [ "$1" = "" ] ; then
	help
	exit 1
    fi
    echo "stop detector $1!"
    if [ `grep -c "^$1" .detected` -eq 1 ] ; then #nice
	kill $1
	sed -i "/^$1/d" .detected
	echo "kill done!"
    else
	echo "detector $1 is not exist!"
    fi
}
function status()
{
    sed -i /^$/d .detected
    echo -e "DPID\tPID COMMADN\n`cat .detected`"
}
function stopall()
{
    for pid in `awk '{print $1}' .detected`
    do
        echo "kill $pid"
        kill $pid
    done
    echo "" > .detected
}
function daemon()
{
    if [ "$1" = "" ] ; then
	help
	exit 1
    fi
    while true ; do
	sleep 10
	ps -p "$1"
	if [ "$?" -ne 0 ] ; then
	    ret=`egrep "	$1 " .detected | cut -d "	" -f2-`
	    ip=`ifconfig eth0 | grep -w inet | cut -d":" -f2 | cut -d" " -f1`
	    user=`whoami`
	    python send_mail.py -s detector_pid -l "process $1 is not exist!<br />location:$user@$ip<br />detail:$ret" -t jingcheng.lijc
	    sed -i "/	$1 /d" .detected
	    exit 1
	fi
    done
}

#start!
if [ "$#" -eq 0 -o "$1" = "-h" ] ; then
    help
    exit 1
fi
init
case $1 in
    "start" ) start $2;break;;
    "stop" ) stop $2;break;;
    "status" ) status;break;;
    "stopall" ) stopall;break;;
    "daemon" ) daemon $2;break;;
    * ) echo "Input Error!. -h for help";exit 1;;
esac


补充说明:

1、程序中有部分tab由于显示原因变成了空格。

2、email部分是个脚本,需要自己准备。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值