From cabf2c10a992c490d91b4ec3b0364d23a66a0935 Mon Sep 17 00:00:00 2001 From: Samuel Pua Date: Sun, 13 May 2018 12:23:53 +0800 Subject: [PATCH] included source --- check-process.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 check-process.py diff --git a/check-process.py b/check-process.py new file mode 100644 index 0000000..ca1e8a3 --- /dev/null +++ b/check-process.py @@ -0,0 +1,61 @@ +#!/usr/bin/python3 + +import os +import sys +import datetime +import subprocess + +def main(): + seconds=10 + + if len(sys.argv)==1: + print("Running "+sys.argv[0]+" for 10 seconds") + print("Usage:") + print(sys.argv[0]+" ") + + elif len(sys.argv)==2: + seconds=int(sys.argv[1]) + + elif len(sys.argv)>2: + print("Wrong usage") + print("Usage:") + print(sys.argv[0]+" ") + sys.exit(0) + + #start doing work + startTime=datetime.datetime.now() + + + while datetime.datetime.now()-startTime < datetime.timedelta(0,seconds): + oldProcs=[] + newProcs=[] + + #old + checkProc = subprocess.Popen(["/bin/bash", "-c", "ps -eF | awk '{print $2}'"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + for line in checkProc.stdout: + oldProcs.append(line[:-1].decode("utf8")) + + #new + checkProc = subprocess.Popen(["/bin/bash", "-c", "ps -eF | awk '{print $2}'"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + for line in checkProc.stdout: + newProcs.append(line[:-1].decode("utf8")) + + #check diff + for i in newProcs: + if not(i in oldProcs): + sys.stdout.write("New: ") + sys.stdout.flush() + checkProc = subprocess.Popen(["/bin/bash", "-c", "ps -eF | grep "+i], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + for line in checkProc.stdout: + print(line[:-1].decode("utf8")) + + for i in oldProcs: + if not(i in newProcs): + sys.stdout.write("Died: ") + sys.stdout.flush() + checkProc = subprocess.Popen(["/bin/bash", "-c", "ps -eF | grep "+i], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + for line in checkProc.stdout: + print(line[:-1].decode("utf8")) + +if __name__ == "__main__": + main()