Process PID Monitoring

Source: Internet
Author: User

#!/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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.