python开发: linux进程打开的文件数

 1 #!/usr/bin/env python
 2 #-*- coding:utf-8 -*-
 3 
 4 ''' 统计linux打开的文件数 '''
 5 
 6 import os
 7 import sys
 8 import subprocess
 9 
10 def getPidList(proc):
11     cmd = '''/sbin/pidof %s''' % proc
12     p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
13     pidList = p.stdout.read().split()
14     return pidList
15 
16 
17 def getFileCount(pidList):
18     sum = 0
19     for pid in pidList:
20         cmd = '''/usr/sbin/lsof -p %s|wc -l''' % pid
21         p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
22         c = p.stdout.read()
23         sum += int(c)
24     return sum
25 
26 if __name__ == '__main__':
27     proc = sys.argv[1]
28     pidList = getPidList(proc)
29     totalCount = getFileCount(pidList)
30     print('%s打开的文件数:%d' % (proc.capitalize(), totalCount))

lsof命令不识别则需要系统安装

posted @ 2020-06-22 15:48  jiangcheng_15  阅读(296)  评论(0编辑  收藏  举报