psutil模块在获取进程信息方面也提供了很好的支持,包括使用psutil.pids()方法获取所有进程PID
{#7031-1542185592715}1)进程信息
文章源自小柒网-https://www.yangxingzhen.cn/1617.html
{#1220-1542185011291}#列出所有进程PID
文章源自小柒网-https://www.yangxingzhen.cn/1617.html
{#4170-1542184715370}>>> import psutil
文章源自小柒网-https://www.yangxingzhen.cn/1617.html
{#8673-1542184988164}>>> psutil.pids()
文章源自小柒网-https://www.yangxingzhen.cn/1617.html
{#4580-1542184988165}[1, 2, 3, 5, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 25, 26, 27, 28, 36, 38, 39, 40, 59, 91, 227, 233, 234, 235, 236, 239, 251, 256, 257, 324, 430, 451, 456, 480, 3997, 4640, 12449, 12495, 13428, 13481, 13508, 13605, 13664, 13682, 13692, 15000, 19846, 23371, 23422, 24774, 25907, 26079, 26081, 26192, 26194, 26242, 26279, 26280, 26322, 30759]
文章源自小柒网-https://www.yangxingzhen.cn/1617.html
{#1311-1542185037962}#实例化一个Process对象,参数为一进程PID
文章源自小柒网-https://www.yangxingzhen.cn/1617.html
{#6721-1542184988165}>>> p = psutil.Process(4640)
文章源自小柒网-https://www.yangxingzhen.cn/1617.html
{#2430-1542185040683}#进程名
文章源自小柒网-https://www.yangxingzhen.cn/1617.html
{#2448-1542184988165}>>> p.name()
文章源自小柒网-https://www.yangxingzhen.cn/1617.html
{#4050-1542184988165}'sshd'
文章源自小柒网-https://www.yangxingzhen.cn/1617.html
{#1282-1542185095899}#进程bin路径
{#5425-1542184988165}>>> p.exe()
{#2539-1542184988165}'/usr/sbin/sshd'
{#8030-1542185107564}#进程工作目录觉得路径
{#3456-1542184988165}>>> p.cwd()
{#8993-1542184988165}'/'
{#4070-1542185112890}#进程状态
{#9826-1542184988165}>>> p.status()
{#8336-1542184988165}'sleeping'
{#6056-1542185114699}#进程创建时间,时间戳格式
{#9618-1542184988165}>>> p.create_time()
{#5070-1542184988165}1533908809.82
{#9147-1542185117098}#进程uid信息
{#8485-1542184988165}>>> p.uids()
{#5033-1542184988165}puids(real=0, effective=0, saved=0)
{#1994-1542185243115}#进程gid信息
{#9510-1542184988165}>>> p.gids()
{#6345-1542184988165}pgids(real=0, effective=0, saved=0)
{#6097-1542185245162}#进程CPU时间信息,包括user、system两个CPU时间
{#5779-1542184988165}>>> p.cpu_times()
{#4482-1542184988165}pcputimes(user=0.0, system=0.02)
{#1053-1542185247579}#get进程CPU亲和度,如要设置进程CPU亲和度,将CPU号作为参数即可
{#9050-1542184988165}>>> p.cpu_affinity()
{#1484-1542184988165}[0]
{#3492-1542185249323}#进程内存利用率
{#5949-1542184988165}>>> p.memory_percent()
{#7369-1542184988165}0.22893646482172475
{#2939-1542185549882}#进程内存rss、vms信息
{#7820-1542184988165}>>> p.memory_info()
{#8517-1542184988165}pmem(rss=4415488, vms=115519488)
{#2355-1542185555405}#进程IO信息,包括读写IO数及字节数
{#2643-1542184988165}>>> p.io_counters()
{#4064-1542184988165}pio(read_count=67866, write_count=52290, read_bytes=1716224, write_bytes=218906624)
{#3280-1542185433131}#返回打开进程socket的namedutples列表,包括fs、family、laddr等信息
{#4649-1542184988165}>>> p.connections()
{#3391-1542184988165}[pconn(fd=3, family=2, type=1, laddr=('0.0.0.0', 1993), raddr=(), status='LISTEN')]
{#6624-1542185435851}#进程开启的线程数
{#3944-1542184988165}>>> p.num_threads()
{#5417-1542184988165}1
{#7311-1542185738427}2)popen类的使用
{#6566-1542185945532}#通过psutil的Popen方法启动的应用程序,可以跟踪该程序运行的所有相关信息
{#4520-1542185749259}>>> import psutil
{#8268-1542185943315}>>> from subprocess import PIPE
{#5830-1542185943315}>>> p = psutil.Popen(["/usr/bin/python", "-c", "print('hello')"], stdout=PIPE)
{#6696-1542185943315}>>> p.name()
{#6022-1542185943315}'python'
{#2638-1542185943315}>>> p.username()
{#8886-1542185943315}'root'
{#6694-1542185943315}>>> p.communicate()
{#7228-1542185943315}('hello\n', None)
继续阅读
历史上的今天
11 月
14