forked from gqdw/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor1.py
More file actions
36 lines (32 loc) · 784 Bytes
/
monitor1.py
File metadata and controls
36 lines (32 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import re
import os
import subprocess
import time
# cmd = "ps axu|grep java|grep -v grep |awk '{print $3,$4}'"
def do_with_ps(ps):
user = ps[0]
pid = ps[1]
cpu = ps[2]
mem = ps[3]
vsz = ps[4]
command = ps[-1]
f = open('out.txt','a')
# print >> f,"%s %s %s %s %s" % (user, pid, cpu, mem, command )
f.write("%s %s %s %s %s %s\n" % (user, pid, cpu, mem, command, vsz ))
f.close()
print "%s %s %s %s %s" % (user, pid, cpu, mem, command )
def main():
cmd = 'ps axu'
while True:
child1 = subprocess.Popen(cmd.split(),stdout=subprocess.PIPE)
res = child1.communicate()
# print res[0]
ps = res[0].split('\n')
for line in ps:
if re.search('java', line):
# print line
ps = line.split()
do_with_ps(ps)
time.sleep(5)
if __name__ == '__main__':
main()