#!/usr/bin/env python
#-*- coding:utf-8 -*- ''' 统计nginx进程占用的物理内存 ''' import os
import sys
import subprocess def getPidList(proc):
cmd = '''/usr/sbin/pidof %s''' % proc
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
pidList = p.stdout.read().split()
return pidList def getMemSize(pidList):
sum = 0
for pid in pidList:
filename = os.path.join('/proc', pid, 'status')
with open(filename) as fd:
for line in fd.readlines():
if line.startswith('VmRSS'):
memSize = int(line.split()[1])
sum += memSize
break
return sum if __name__ == '__main__':
proc = sys.argv[1]
pidList = getPidList(proc)
totalMem = getMemSize(pidList)
print('%s占用物理内存:%sK' % (proc.capitalize(), totalMem))
[root@localhost ~]$ python 1.py nginx
Nginx占用物理内存:8056K
05-23 18:36