python小练--获取pid和mem

ExpandedBlockStart.gif 代码
 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 

 

转载于:https://www.cnblogs.com/yd1227/archive/2010/06/14/1758186.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值