代码
1 #!/usr/bin/env python
2 #coding=utf-8
3 #注意修改get_mem函数中,splite取的列要对应你的目标
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 except Exception,e:
19 # print e
20 return pid
21 def get_Mem(pid):
22 cmd_top = 'top -p %s -b -n 1 | tail -n 2 | head -n 1'%pid
23 mem = os.popen(cmd_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
2 #coding=utf-8
3 #注意修改get_mem函数中,splite取的列要对应你的目标
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 except Exception,e:
19 # print e
20 return pid
21 def get_Mem(pid):
22 cmd_top = 'top -p %s -b -n 1 | tail -n 2 | head -n 1'%pid
23 mem = os.popen(cmd_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