1.python检测linux下进程的运行
#coding=utf8import sys,osstatus_ok = 0status_critical = 2def check(Pid): dir,file = os.path.split(Pid) if not(os.path.exists(Pid)): print 'Critical - %s is stop.' % file sys.exit(status_critical) else: print 'OK - %s is running.' % file sys.exit(status_ok)if __name__ == '__main__': #pid = "/var/run/sshd.pid" pid = sys.argv[1] check(pid)
运行:python check_daemon.py /var/run/sshd.pid
2.shell检测linux进程运行
#!/bin/sh daemon=$1status_ok=0status_critical=2pid=`ps -ef | grep -v grep | grep -v $0| grep $daemon| sed -n '1P'|awk '{print $2}'`if [ -z $pid ]; then echo "Critical - $daemon is stop." exit $status_criticalelse echo "OK - $daemon is running." exit $status_okfi
sh check_daemon.sh sshd
3.linux一般用init管理进程,不过有一个用python写的模块(supervisor)也能很优秀的管理进程,而且可以管理任意运行脚本。