Code
1 #! /Usr/bin/env python
2 # coding = UTF-8
3 # note that in the get_mem function, the splite column must correspond to your target
4 import OS
5 import sys
6
7 def get_Pid (process_name ):
8 cmd = "ps-C % s | grep-v CMD | awk '{print $1}'" % process_name
9 # print cmd
10 try:
11 pid = OS. popen (cmd). read ()
12 if pid:
13 print 'the pid of process_name: '% process_name +' is ', pid
14 return pid
15 else:
16 print 'sorry to get pid, maybe the process_name is wrong? '
17 return None
18 failed t Exception, e:
19 # print e
20 return pid
21 def get_Mem (pid ):
22 performance_top = 'top-p % s-B-n 1 | tail-n 2 | head-n 1' % pid
23 mem = OS. popen (kernel _top). read (). split () [4]
24 return mem
25
26 def main (process_name ):
27 PID = get_Pid (process_name)
28 result = get_Mem (PID)
29 print "the process '% s', whose mem is % s" % (process_name, result)
30
31 if _ name _ = '_ main __':
32 process_name = sys. argv [1]
33 main (process_name)
34