python检测linux下运行的进程# -*- coding:utf8 -*-
import subprocess
import sys
status_ok = 0
status_critical = 2
def c(d_name):
cmd = ‘ps -ef|grep %s|grep -v "grep"‘ % d_name
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
if p.wait() == 0:
val = p.stdout.read()
if d_name in val:
print ‘OK - %s daemon is running.‘ % d_name
sys.exit(status_ok)
else:
print ‘Critical - %s daemon is stop.‘ % d_name
sys.exit(status_critical)
if __name__ == ‘__main__‘:
#c(‘sersync2‘)
d_name = sys.argv[1]
c(d_name)
原文:http://xieyusong.blog.51cto.com/4615779/1563407