分析:
# pidof httpd
97144 4574 4573 4572 4571 4570
# cat /proc/97144/status | grep VmRSS
VmRSS: 5032 kB
#!/usr/bin/env python
#coding=utf8
import subprocess
pidcheck = subprocess.Popen('pidof httpd',shell=True,stdout=subprocess.PIPE)
pid = pidcheck.stdout.read().rstrip().split()
memsize = 0
for i in pid:
filename = "/proc/"+i+"/status"
#os.path.join("/proc","i","status") #等同于上条语句
with open(filename,"r") as statusfile:
for line in statusfile:
if line.startswith("VmRSS"):
memsize+=int(line.split()[1])
print "Apache进程占用内存:%sk" % memsize
with open("/proc/meminfo","r") as memfile:
for line in memfile:
if line.startswith("MemTotal"):
memtotal = int(line.split()[1])
print "Apache进程内存占用率为:%.2f%%" % (memsize/float(memtotal)*100)
运行结果:
# python a.py
Apache进程占用内存大小:20472k
Apache进程内存占用率为:0.25%
本文转自 CrazyWing 51CTO博客,原文链接:http://blog.51cto.com/fklinux/1980631