#!/usr/bin/evn python# -*- coding:utf-8 -*-#Author = ' June ' "" "can monitor multiple processes simultaneously" "Import sysimport reimport osimport linecachetry: import psutilexcept importerror as e: print e sys.exit () def process (processname): plist = [] getplist = psutil.process_iter () for pro in getplist: p = str (PRO) re1 = Re.compile (Processname, re. I) if re1.search (p): plist.append (str (P). Split (' pid= ') [1].split (', ') [0]) return plistdef file_size_check (logfile): if os.path.isFile (logfile): if os.path.getsize (logfile) > 2700000: f = open (LogFile, ' W ') f.write ("") f.close () Def main (pname, filename, Pnumber=1): readline = pnumber * 2 logfile = os.path.dirname (Os.path.realpath (__file__)) + os.sep + filename file_size_check (logfile) pidlist = process (pname) chpidlist = [] lines = linecache.getlines (logfile) [- Readline:] taillines = [line.strip (' \ n ') for line in lines] pl&Nbsp;= [p for p in pidlist if taillines.count (P) == 2] f = open (logfile, ' a ') if len (taillines) < readline: print "check log less Than %s line " % readline [f.write (p + ' \ n ') for p in pidlist] f.close () sys.exit (0) elif len (PL) == pnumber: print "Check %s process ok, pidis : " % pname, pidlist [f.write (P + ' \ n ') for p in pidlist] F.close () &Nbsp; sys.exit (0) else: for p in pidlist: If taillines.count (P) != 2: chpidlist.append (p) print " check %s process is change, change pid is ::: " % pname, chpidlist [f.write (p + ' \ n ') for p in pidlist] f.close () sys.exit (1) if __name__ == ' __main__ ': if len ( SYS.ARGV) == 3: proname = sys.argv[1] &Nbsp; pnum = int (sys.argv[2]) logf = " Check_%s_pid " % proname main (PRONAME,&NBSP;LOGF, pnum) elif len (SYS.ARGV) == 2: proname = sys.argv[1] logf = " Check_%s_pid " % proname main (PRONAME,&NBSP;LOGF) else: print "usage:%s processname [processnumber] " % sys.argv[0] sys.exit (2)
This article is from the "Embrace Open source" blog, be sure to keep this source http://junit.blog.51cto.com/9897514/1643598
Process PID Monitoring