1. 程式人生 > >Python 查詢 process 的開檔數量

Python 查詢 process 的開檔數量

上次使用shell script 的心得是,shell script 存取檔案還有關於字串處理沒有python方便,所以這次查詢 process 開檔數量就改用python 來實作。

上次的shell script:

#!/usr/bin/env python
import subprocess
child = subprocess.Popen(["ps -ef | grep server.py | grep -v grep | awk '{print $2}'"], stdout=subprocess.PIPE, shell=True)
pid = child.communicate()[0]
pid = pid.replace("\n","")
print "pid:",pid
cmd = "ls -l /proc/%s/fd | wc -l" % (pid)
print "cmd:", cmd
child = subprocess.Popen([cmd], stdout=subprocess.PIPE, shell=True)
fd = child.communicate()[0]
fd = fd.replace("\n","")
print "fd:",fd