本文共 578 字,大约阅读时间需要 1 分钟。
#!/usr/bin/python
python监控linux进程数量工作原理:基于/proc 文件系统
import os
def get_all_pid():
return [d for d in os.listdir("/proc") if d.isdigit()]def get_cmdline(pid=[]):
ret = []for pid_dir in pid:with open("/proc/{}/cmdline".format(pid_dir)) as f:cmdline = f.readline()if cmdline:ret.append(cmdline)return retdef get_process_num(process_name):
pids = get_all_pid()cmdlines = get_cmdline(pids)num = 0for line in cmdlines:if process_name in line:num += 1return numif name == "main":
print get_process_num("php-fpm")转载于:https://blog.51cto.com/12868380/2137402