I have written a small code which is capturing the "rm" process running on the OS.. Below is the code
bash-4.1$ cat CheckRM1.py
#!/usr/bin/python
import subprocess
def Check_Ps():
ps = subprocess.Popen(['ps', '-eo' 'pid,user,args,etime='], stdout=subprocess.PIPE)
output = ps.communicate()[0]
for line in output.splitlines():
if 'rm' in line:
pss = (line)
print pss
Check_Ps()
When i run the code, it generate the output in the below form: this basically giving the process ID number(PID) , user name, command & at last the total time since the process running. This time basically uses the format
[[dd-]hh:]mm:ss
. In my case its running more than 9 mins.
bash-4.1$ ./CheckRM1.py
22908 karn rm -i e ee q r w 09:19
Now i'm looking to search the process which are running more 30 mins and Just print them out and send a mail. So, any idea how to capture only process running more than 30 mins, will be appreciated.
NOte: I'm using python 2.7 .
Question : ... how to capture only process running more than 30 mins
You are already close, split(..
the time from line by blanks, eg
import re
time_str = line.split(' ')[-1]
# format [[dd-]hh:]mm:ss
time_items = [int(e) for e in re.split('[-:]', time_str)]
dd = 0
if len(time_items) == 2:
hh = 0
mm,ss = time_items
elif len(time_items) == 3:
hh, mm, ss = time_items
else:
dd,hh,mm,ss = time_items
print('Process is running {} Days {} Hours {} Minutes {} Seconds'.format(dd,hh,mm,ss))
time_minutes = (hh*60) + mm
if time_minutes >= 30:
# do report
Tested with Python:3.4.2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.