获取所有进程pid
[root@bogon python_auto]# vim process.py
!/usr/bin/env python
import psutil
pids = psutil.pids()
print pids
执行文件,查看系统上所有进程pid
[root@bogon python_auto]# python process.py
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 38, 39, 41, 42, 72, 148, 149, 156, 157, 158, 287, 288, 299, 381, 744, 916, 917, 951, 1203, 1228, 1298, 1309, 1313, 1330, 1331, 1356, 1365, 1366, 1409, 1416, 1456, 1538, 1551, 1562, 1570, 1581, 1629, 1635, 1637, 1639, 1641, 1643, 1653, 1654, 1664, 1667, 1683, 1753, 1759, 1799, 1810, 1816, 1826, 1836, 1844, 1845, 1863, 1871, 1875, 1877, 1897, 1901, 1902, 1905, 1906, 1908, 1917, 1918, 1920, 1922, 1923, 1925, 1928, 1931, 1934, 1936, 1938, 1939, 1942, 1944, 1953, 1955, 1958, 1977, 1981, 1982, 1983, 2010, 2012, 2013, 2014, 2029, 21686, 21816, 27926, 33028, 33816, 33829, 33834, 40565, 41576, 45249, 45253, 45434, 45439, 45605]
[root@bogon python_auto]#
利用进程的pid得到进程名
[root@bogon python_auto]# vim process.py
!/usr/bin/env python
import psutil
pids = psutil.pids()
print pids
p_name = psutil.Process(2)
print p_name
查看pid是2的进程名
[root@bogon python_auto]# python process.py
psutil.Process(pid=2, name='kthreadd')
[root@bogon python_auto]#
利用进程号,查看进程的bin路径
[root@bogon python_auto]# vim process.py
!/usr/bin/env python
import psutil
pids = psutil.pids()
print pids
p_bin = psutil.Process(2029).exe()
print p_bin
查看进程路径
[root@bogon python_auto]# python process.py
/sbin/dhclient
利用进程号,查看进程工作目录绝对路径
[root@bogon python_auto]# vim process.py
!/usr/bin/env python
import psutil
pids = psutil.pids()
print pids
p_bin = psutil.Process(2029).exe()
p_cwd = psutil.Process(2029).cwd()
print p_cwd
查看进程号是2029的进程的工作目录的绝对路径
[root@bogon python_auto]# python process.py
/
利用进程号,查看进程的状态
[root@bogon python_auto]# python process.py
/
[root@bogon python_auto]# vim process.py
!/usr/bin/env python
import psutil
pids = psutil.pids()
print pids
p_bin = psutil.Process(2029).exe()
p_cwd = psutil.Process(2029).cwd()
p_status = psutil.Process(2029).status()
print p_status
查看进程号是2029的进程的状态
[root@bogon python_auto]# python process.py
sleeping
利用进程号,查看进程创建时间
[root@bogon python_auto]# vim process.py
!/usr/bin/env python
import psutil
p_create_time = psutil.Process(2029).create_time()
print p_create_time
查看进程创建时间(时间戳形式)
[root@bogon python_auto]# python process.py
1452343704.16
利用进程号,查看进程uid信息
[root@bogon python_auto]# vim process.py
!/usr/bin/env python
import psutil
pids = psutil.pids()
print pids
p_bin = psutil.Process(2029).exe()
p_cwd = psutil.Process(2029).cwd()
p_status = psutil.Process(2029).status()
print p_status
p_create_time = psutil.Process(2029).create_time()
print p_create_time
p_uids = psutil.Process(2029).uids()
print p_uids
查看进程uid信息
[root@bogon python_auto]# python process.py
puids(real=0, effective=0, saved=0)
[root@bogon python_auto]#
利用进程号查看进程gid信息
[root@bogon python_auto]# vim process.py
print pids
p_bin = psutil.Process(2029).exe()
p_cwd = psutil.Process(2029).cwd()
p_status = psutil.Process(2029).status()
print p_status
p_create_time = psutil.Process(2029).create_time()
print p_create_time
p_uids = psutil.Process(2029).uids()
print p_uids
p_gids = psutil.Process(2029).gids()
print p_gids
查看进程组id信息
[root@bogon python_auto]# python process.py
pgids(real=0, effective=0, saved=0)
利用进程号,查看进程cpu时间信息
p_cpu_times = psutil.Process(2029).cpu_times()
print p_cpu_times
查看进程cpu时间
[root@bogon python_auto]# python process.py
pcputimes(user=0.16, system=4.8099999999999996)
利用进程号,查看进程利用率
p_mem_percent =psutil.Process(2029).memory_percent()
print p_mem_percent
查看进程利用率
[root@bogon python_auto]# python process.py
0.126243015814
利用进程号,查看进程io信息
p_io = psutil.Process(2029).io_counters()
print p_io
查看:
[root@bogon python_auto]# python process.py
pio(read_count=259, write_count=16074, read_bytes=2347008, write_bytes=45056)
利用进程号,查看进程开启的线程数
p_num_threads = psutil.Process(2029).num_threads()
print p_num_threads
查看进程开启的线程数
[root@bogon python_auto]# python process.py
1