python监控linux进程数量
工作原理:基于/proc 文件系统
#!/usr/bin/python # coding:utf-8 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 ret def get_process_num(process_name): pids = get_all_pid() cmdlines = get_cmdline(pids) num = 0 for line in cmdlines: if process_name in line: num += 1 return num if __name__ == "__main__": print get_process_num("php-fpm")