README.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. | |downloads| |stars| |forks| |contributors| |coverage| |quality|
  2. | |version| |py-versions| |packages| |license|
  3. | |github-actions| |appveyor| |doc| |twitter| |tidelift|
  4. .. |downloads| image:: https://img.shields.io/pypi/dm/psutil.svg
  5. :target: https://pepy.tech/project/psutil
  6. :alt: Downloads
  7. .. |stars| image:: https://img.shields.io/github/stars/giampaolo/psutil.svg
  8. :target: https://github.com/giampaolo/psutil/stargazers
  9. :alt: Github stars
  10. .. |forks| image:: https://img.shields.io/github/forks/giampaolo/psutil.svg
  11. :target: https://github.com/giampaolo/psutil/network/members
  12. :alt: Github forks
  13. .. |contributors| image:: https://img.shields.io/github/contributors/giampaolo/psutil.svg
  14. :target: https://github.com/giampaolo/psutil/graphs/contributors
  15. :alt: Contributors
  16. .. |quality| image:: https://img.shields.io/codacy/grade/ce63e7f7f69d44b5b59682196e6fbfca.svg
  17. :target: https://www.codacy.com/app/g-rodola/psutil?utm_source=github.com&utm_medium=referral&utm_content=giampaolo/psutil&utm_campaign=Badge_Grade
  18. :alt: Code quality
  19. .. |github-actions| image:: https://img.shields.io/github/workflow/status/giampaolo/psutil/CI?label=Linux%2C%20macOS%2C%20FreeBSD
  20. :target: https://github.com/giampaolo/psutil/actions?query=workflow%3ACI
  21. :alt: Linux, macOS, Windows tests
  22. .. |appveyor| image:: https://img.shields.io/appveyor/ci/giampaolo/psutil/master.svg?maxAge=3600&label=Windows
  23. :target: https://ci.appveyor.com/project/giampaolo/psutil
  24. :alt: Windows tests (Appveyor)
  25. .. |coverage| image:: https://coveralls.io/repos/github/giampaolo/psutil/badge.svg?branch=master
  26. :target: https://coveralls.io/github/giampaolo/psutil?branch=master
  27. :alt: Test coverage (coverall.io)
  28. .. |doc| image:: https://readthedocs.org/projects/psutil/badge/?version=latest
  29. :target: http://psutil.readthedocs.io/en/latest/?badge=latest
  30. :alt: Documentation Status
  31. .. |version| image:: https://img.shields.io/pypi/v/psutil.svg?label=pypi
  32. :target: https://pypi.org/project/psutil
  33. :alt: Latest version
  34. .. |py-versions| image:: https://img.shields.io/pypi/pyversions/psutil.svg
  35. :target: https://pypi.org/project/psutil
  36. :alt: Supported Python versions
  37. .. |packages| image:: https://repology.org/badge/tiny-repos/python:psutil.svg
  38. :target: https://repology.org/metapackage/python:psutil/versions
  39. :alt: Binary packages
  40. .. |license| image:: https://img.shields.io/pypi/l/psutil.svg
  41. :target: https://github.com/giampaolo/psutil/blob/master/LICENSE
  42. :alt: License
  43. .. |twitter| image:: https://img.shields.io/twitter/follow/grodola.svg?label=follow&style=flat&logo=twitter&logoColor=4FADFF
  44. :target: https://twitter.com/grodola
  45. :alt: Twitter Follow
  46. .. |tidelift| image:: https://tidelift.com/badges/github/giampaolo/psutil?style=flat
  47. :target: https://tidelift.com/subscription/pkg/pypi-psutil?utm_source=pypi-psutil&utm_medium=referral&utm_campaign=readme
  48. :alt: Tidelift
  49. -----
  50. .. raw:: html
  51. <div align="center">
  52. <a href="https://github.com/giampaolo/psutil"><img src="https://github.com/giampaolo/psutil/raw/master/docs/_static/psutil-logo.png" /></a>
  53. <br />
  54. <br />
  55. <a href="https://github.com/giampaolo/psutil"><b>Home</b></a>&nbsp;&nbsp;&nbsp;
  56. <a href="https://github.com/giampaolo/psutil/blob/master/INSTALL.rst"><b>Install</b></a>&nbsp;&nbsp;&nbsp;
  57. <a href="https://psutil.readthedocs.io/"><b>Documentation</b></a>&nbsp;&nbsp;&nbsp;
  58. <a href="https://pypi.org/project/psutil/#files"><b>Download</b></a>&nbsp;&nbsp;&nbsp;
  59. <a href="https://groups.google.com/g/psutil"><b>Forum</b></a>&nbsp;&nbsp;&nbsp;
  60. <a href="https://gmpy.dev/tags/psutil"><b>Blog</b></a>&nbsp;&nbsp;&nbsp;
  61. <a href="#funding"><b>Funding</b></a>&nbsp;&nbsp;&nbsp;
  62. <a href="https://github.com/giampaolo/psutil/blob/master/HISTORY.rst"><b>What's new</b></a>&nbsp;&nbsp;&nbsp;
  63. </div>
  64. Summary
  65. =======
  66. psutil (process and system utilities) is a cross-platform library for
  67. retrieving information on **running processes** and **system utilization**
  68. (CPU, memory, disks, network, sensors) in Python.
  69. It is useful mainly for **system monitoring**, **profiling and limiting process
  70. resources** and **management of running processes**.
  71. It implements many functionalities offered by classic UNIX command line tools
  72. such as *ps, top, iotop, lsof, netstat, ifconfig, free* and others.
  73. psutil currently supports the following platforms:
  74. - **Linux**
  75. - **Windows**
  76. - **macOS**
  77. - **FreeBSD, OpenBSD**, **NetBSD**
  78. - **Sun Solaris**
  79. - **AIX**
  80. Supported Python versions are **2.6**, **2.7**, **3.4+** and
  81. `PyPy <http://pypy.org/>`__.
  82. Funding
  83. =======
  84. While psutil is free software and will always be, the project would benefit
  85. immensely from some funding.
  86. Keeping up with bug reports and maintenance has become hardly sustainable for
  87. me alone in terms of time.
  88. If you're a company that's making significant use of psutil you can consider
  89. becoming a sponsor via `GitHub <https://github.com/sponsors/giampaolo>`__,
  90. `Open Collective <https://opencollective.com/psutil>`__ or
  91. `PayPal <https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=A9ZS7PKKRM3S8>`__
  92. and have your logo displayed in here and psutil `doc <https://psutil.readthedocs.io>`__.
  93. Sponsors
  94. ========
  95. .. raw:: html
  96. <div>
  97. <a href="https://tidelift.com/subscription/pkg/pypi-psutil?utm_source=pypi-psutil&utm_medium=referral&utm_campaign=readme">
  98. <img src="https://github.com/giampaolo/psutil/raw/master/docs/_static/tidelift-logo.png" />
  99. </a>
  100. </div>
  101. <sup><a href="https://github.com/sponsors/giampaolo">add your logo</a></sup>
  102. Supporters
  103. ==========
  104. None yet.
  105. .. raw:: html
  106. <sup><a href="https://github.com/sponsors/giampaolo">add your avatar</a></sup>
  107. Contributing
  108. ============
  109. See `CONTRIBUTING.md <https://github.com/giampaolo/psutil/blob/master/CONTRIBUTING.md>`__ guidelines.
  110. Example usages
  111. ==============
  112. This represents pretty much the whole psutil API.
  113. CPU
  114. ---
  115. .. code-block:: python
  116. >>> import psutil
  117. >>>
  118. >>> psutil.cpu_times()
  119. scputimes(user=3961.46, nice=169.729, system=2150.659, idle=16900.540, iowait=629.59, irq=0.0, softirq=19.42, steal=0.0, guest=0, nice=0.0)
  120. >>>
  121. >>> for x in range(3):
  122. ... psutil.cpu_percent(interval=1)
  123. ...
  124. 4.0
  125. 5.9
  126. 3.8
  127. >>>
  128. >>> for x in range(3):
  129. ... psutil.cpu_percent(interval=1, percpu=True)
  130. ...
  131. [4.0, 6.9, 3.7, 9.2]
  132. [7.0, 8.5, 2.4, 2.1]
  133. [1.2, 9.0, 9.9, 7.2]
  134. >>>
  135. >>> for x in range(3):
  136. ... psutil.cpu_times_percent(interval=1, percpu=False)
  137. ...
  138. scputimes(user=1.5, nice=0.0, system=0.5, idle=96.5, iowait=1.5, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
  139. scputimes(user=1.0, nice=0.0, system=0.0, idle=99.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
  140. scputimes(user=2.0, nice=0.0, system=0.0, idle=98.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
  141. >>>
  142. >>> psutil.cpu_count()
  143. 4
  144. >>> psutil.cpu_count(logical=False)
  145. 2
  146. >>>
  147. >>> psutil.cpu_stats()
  148. scpustats(ctx_switches=20455687, interrupts=6598984, soft_interrupts=2134212, syscalls=0)
  149. >>>
  150. >>> psutil.cpu_freq()
  151. scpufreq(current=931.42925, min=800.0, max=3500.0)
  152. >>>
  153. >>> psutil.getloadavg() # also on Windows (emulated)
  154. (3.14, 3.89, 4.67)
  155. Memory
  156. ------
  157. .. code-block:: python
  158. >>> psutil.virtual_memory()
  159. svmem(total=10367352832, available=6472179712, percent=37.6, used=8186245120, free=2181107712, active=4748992512, inactive=2758115328, buffers=790724608, cached=3500347392, shared=787554304)
  160. >>> psutil.swap_memory()
  161. sswap(total=2097147904, used=296128512, free=1801019392, percent=14.1, sin=304193536, sout=677842944)
  162. >>>
  163. Disks
  164. -----
  165. .. code-block:: python
  166. >>> psutil.disk_partitions()
  167. [sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,nosuid', maxfile=255, maxpath=4096),
  168. sdiskpart(device='/dev/sda2', mountpoint='/home', fstype='ext, opts='rw', maxfile=255, maxpath=4096)]
  169. >>>
  170. >>> psutil.disk_usage('/')
  171. sdiskusage(total=21378641920, used=4809781248, free=15482871808, percent=22.5)
  172. >>>
  173. >>> psutil.disk_io_counters(perdisk=False)
  174. sdiskio(read_count=719566, write_count=1082197, read_bytes=18626220032, write_bytes=24081764352, read_time=5023392, write_time=63199568, read_merged_count=619166, write_merged_count=812396, busy_time=4523412)
  175. >>>
  176. Network
  177. -------
  178. .. code-block:: python
  179. >>> psutil.net_io_counters(pernic=True)
  180. {'eth0': netio(bytes_sent=485291293, bytes_recv=6004858642, packets_sent=3251564, packets_recv=4787798, errin=0, errout=0, dropin=0, dropout=0),
  181. 'lo': netio(bytes_sent=2838627, bytes_recv=2838627, packets_sent=30567, packets_recv=30567, errin=0, errout=0, dropin=0, dropout=0)}
  182. >>>
  183. >>> psutil.net_connections(kind='tcp')
  184. [sconn(fd=115, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=48776), raddr=addr(ip='93.186.135.91', port=80), status='ESTABLISHED', pid=1254),
  185. sconn(fd=117, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=43761), raddr=addr(ip='72.14.234.100', port=80), status='CLOSING', pid=2987),
  186. ...]
  187. >>>
  188. >>> psutil.net_if_addrs()
  189. {'lo': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast='127.0.0.1', ptp=None),
  190. snicaddr(family=<AddressFamily.AF_INET6: 10>, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None, ptp=None),
  191. snicaddr(family=<AddressFamily.AF_LINK: 17>, address='00:00:00:00:00:00', netmask=None, broadcast='00:00:00:00:00:00', ptp=None)],
  192. 'wlan0': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='192.168.1.3', netmask='255.255.255.0', broadcast='192.168.1.255', ptp=None),
  193. snicaddr(family=<AddressFamily.AF_INET6: 10>, address='fe80::c685:8ff:fe45:641%wlan0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None),
  194. snicaddr(family=<AddressFamily.AF_LINK: 17>, address='c4:85:08:45:06:41', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]}
  195. >>>
  196. >>> psutil.net_if_stats()
  197. {'lo': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=65536),
  198. 'wlan0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=100, mtu=1500)}
  199. >>>
  200. Sensors
  201. -------
  202. .. code-block:: python
  203. >>> import psutil
  204. >>> psutil.sensors_temperatures()
  205. {'acpitz': [shwtemp(label='', current=47.0, high=103.0, critical=103.0)],
  206. 'asus': [shwtemp(label='', current=47.0, high=None, critical=None)],
  207. 'coretemp': [shwtemp(label='Physical id 0', current=52.0, high=100.0, critical=100.0),
  208. shwtemp(label='Core 0', current=45.0, high=100.0, critical=100.0)]}
  209. >>>
  210. >>> psutil.sensors_fans()
  211. {'asus': [sfan(label='cpu_fan', current=3200)]}
  212. >>>
  213. >>> psutil.sensors_battery()
  214. sbattery(percent=93, secsleft=16628, power_plugged=False)
  215. >>>
  216. Other system info
  217. -----------------
  218. .. code-block:: python
  219. >>> import psutil
  220. >>> psutil.users()
  221. [suser(name='giampaolo', terminal='pts/2', host='localhost', started=1340737536.0, pid=1352),
  222. suser(name='giampaolo', terminal='pts/3', host='localhost', started=1340737792.0, pid=1788)]
  223. >>>
  224. >>> psutil.boot_time()
  225. 1365519115.0
  226. >>>
  227. Process management
  228. ------------------
  229. .. code-block:: python
  230. >>> import psutil
  231. >>> psutil.pids()
  232. [1, 2, 3, 4, 5, 6, 7, 46, 48, 50, 51, 178, 182, 222, 223, 224, 268, 1215,
  233. 1216, 1220, 1221, 1243, 1244, 1301, 1601, 2237, 2355, 2637, 2774, 3932,
  234. 4176, 4177, 4185, 4187, 4189, 4225, 4243, 4245, 4263, 4282, 4306, 4311,
  235. 4312, 4313, 4314, 4337, 4339, 4357, 4358, 4363, 4383, 4395, 4408, 4433,
  236. 4443, 4445, 4446, 5167, 5234, 5235, 5252, 5318, 5424, 5644, 6987, 7054,
  237. 7055, 7071]
  238. >>>
  239. >>> p = psutil.Process(7055)
  240. >>> p
  241. psutil.Process(pid=7055, name='python3', status='running', started='09:04:44')
  242. >>> p.name()
  243. 'python'
  244. >>> p.exe()
  245. '/usr/bin/python'
  246. >>> p.cwd()
  247. '/home/giampaolo'
  248. >>> p.cmdline()
  249. ['/usr/bin/python', 'main.py']
  250. >>>
  251. >>> p.pid
  252. 7055
  253. >>> p.ppid()
  254. 7054
  255. >>> p.children(recursive=True)
  256. [psutil.Process(pid=29835, name='python3', status='sleeping', started='11:45:38'),
  257. psutil.Process(pid=29836, name='python3', status='waking', started='11:43:39')]
  258. >>>
  259. >>> p.parent()
  260. psutil.Process(pid=4699, name='bash', status='sleeping', started='09:06:44')
  261. >>> p.parents()
  262. [psutil.Process(pid=4699, name='bash', started='09:06:44'),
  263. psutil.Process(pid=4689, name='gnome-terminal-server', status='sleeping', started='0:06:44'),
  264. psutil.Process(pid=1, name='systemd', status='sleeping', started='05:56:55')]
  265. >>>
  266. >>> p.status()
  267. 'running'
  268. >>> p.username()
  269. 'giampaolo'
  270. >>> p.create_time()
  271. 1267551141.5019531
  272. >>> p.terminal()
  273. '/dev/pts/0'
  274. >>>
  275. >>> p.uids()
  276. puids(real=1000, effective=1000, saved=1000)
  277. >>> p.gids()
  278. pgids(real=1000, effective=1000, saved=1000)
  279. >>>
  280. >>> p.cpu_times()
  281. pcputimes(user=1.02, system=0.31, children_user=0.32, children_system=0.1, iowait=0.0)
  282. >>> p.cpu_percent(interval=1.0)
  283. 12.1
  284. >>> p.cpu_affinity()
  285. [0, 1, 2, 3]
  286. >>> p.cpu_affinity([0, 1]) # set
  287. >>> p.cpu_num()
  288. 1
  289. >>>
  290. >>> p.memory_info()
  291. pmem(rss=10915840, vms=67608576, shared=3313664, text=2310144, lib=0, data=7262208, dirty=0)
  292. >>> p.memory_full_info() # "real" USS memory usage (Linux, macOS, Win only)
  293. pfullmem(rss=10199040, vms=52133888, shared=3887104, text=2867200, lib=0, data=5967872, dirty=0, uss=6545408, pss=6872064, swap=0)
  294. >>> p.memory_percent()
  295. 0.7823
  296. >>> p.memory_maps()
  297. [pmmap_grouped(path='/lib/x8664-linux-gnu/libutil-2.15.so', rss=32768, size=2125824, pss=32768, shared_clean=0, shared_dirty=0, private_clean=20480, private_dirty=12288, referenced=32768, anonymous=12288, swap=0),
  298. pmmap_grouped(path='/lib/x8664-linux-gnu/libc-2.15.so', rss=3821568, size=3842048, pss=3821568, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=3821568, referenced=3575808, anonymous=3821568, swap=0),
  299. pmmap_grouped(path='[heap]', rss=32768, size=139264, pss=32768, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=32768, referenced=32768, anonymous=32768, swap=0),
  300. pmmap_grouped(path='[stack]', rss=2465792, size=2494464, pss=2465792, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=2465792, referenced=2277376, anonymous=2465792, swap=0),
  301. ...]
  302. >>>
  303. >>> p.io_counters()
  304. pio(read_count=478001, write_count=59371, read_bytes=700416, write_bytes=69632, read_chars=456232, write_chars=517543)
  305. >>>
  306. >>> p.open_files()
  307. [popenfile(path='/home/giampaolo/monit.py', fd=3, position=0, mode='r', flags=32768),
  308. popenfile(path='/var/log/monit.log', fd=4, position=235542, mode='a', flags=33793)]
  309. >>>
  310. >>> p.connections(kind='tcp')
  311. [pconn(fd=115, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=48776), raddr=addr(ip='93.186.135.91', port=80), status='ESTABLISHED'),
  312. pconn(fd=117, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=43761), raddr=addr(ip='72.14.234.100', port=80), status='CLOSING')]
  313. >>>
  314. >>> p.num_threads()
  315. 4
  316. >>> p.num_fds()
  317. 8
  318. >>> p.threads()
  319. [pthread(id=5234, user_time=22.5, system_time=9.2891),
  320. pthread(id=5237, user_time=0.0707, system_time=1.1)]
  321. >>>
  322. >>> p.num_ctx_switches()
  323. pctxsw(voluntary=78, involuntary=19)
  324. >>>
  325. >>> p.nice()
  326. 0
  327. >>> p.nice(10) # set
  328. >>>
  329. >>> p.ionice(psutil.IOPRIO_CLASS_IDLE) # IO priority (Win and Linux only)
  330. >>> p.ionice()
  331. pionice(ioclass=<IOPriority.IOPRIO_CLASS_IDLE: 3>, value=0)
  332. >>>
  333. >>> p.rlimit(psutil.RLIMIT_NOFILE, (5, 5)) # set resource limits (Linux only)
  334. >>> p.rlimit(psutil.RLIMIT_NOFILE)
  335. (5, 5)
  336. >>>
  337. >>> p.environ()
  338. {'LC_PAPER': 'it_IT.UTF-8', 'SHELL': '/bin/bash', 'GREP_OPTIONS': '--color=auto',
  339. 'XDG_CONFIG_DIRS': '/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg',
  340. ...}
  341. >>>
  342. >>> p.as_dict()
  343. {'status': 'running', 'num_ctx_switches': pctxsw(voluntary=63, involuntary=1), 'pid': 5457, ...}
  344. >>> p.is_running()
  345. True
  346. >>> p.suspend()
  347. >>> p.resume()
  348. >>>
  349. >>> p.terminate()
  350. >>> p.kill()
  351. >>> p.wait(timeout=3)
  352. <Exitcode.EX_OK: 0>
  353. >>>
  354. >>> psutil.test()
  355. USER PID %CPU %MEM VSZ RSS TTY START TIME COMMAND
  356. root 1 0.0 0.0 24584 2240 Jun17 00:00 init
  357. root 2 0.0 0.0 0 0 Jun17 00:00 kthreadd
  358. ...
  359. giampaolo 31475 0.0 0.0 20760 3024 /dev/pts/0 Jun19 00:00 python2.4
  360. giampaolo 31721 0.0 2.2 773060 181896 00:04 10:30 chrome
  361. root 31763 0.0 0.0 0 0 00:05 00:00 kworker/0:1
  362. >>>
  363. Further process APIs
  364. --------------------
  365. .. code-block:: python
  366. >>> import psutil
  367. >>> for proc in psutil.process_iter(['pid', 'name']):
  368. ... print(proc.info)
  369. ...
  370. {'pid': 1, 'name': 'systemd'}
  371. {'pid': 2, 'name': 'kthreadd'}
  372. {'pid': 3, 'name': 'ksoftirqd/0'}
  373. ...
  374. >>>
  375. >>> psutil.pid_exists(3)
  376. True
  377. >>>
  378. >>> def on_terminate(proc):
  379. ... print("process {} terminated".format(proc))
  380. ...
  381. >>> # waits for multiple processes to terminate
  382. >>> gone, alive = psutil.wait_procs(procs_list, timeout=3, callback=on_terminate)
  383. >>>
  384. Popen wrapper:
  385. .. code-block:: python
  386. >>> import psutil
  387. >>> from subprocess import PIPE
  388. >>> p = psutil.Popen(["/usr/bin/python", "-c", "print('hello')"], stdout=PIPE)
  389. >>> p.name()
  390. 'python'
  391. >>> p.username()
  392. 'giampaolo'
  393. >>> p.communicate()
  394. ('hello\n', None)
  395. >>> p.wait(timeout=2)
  396. 0
  397. >>>
  398. Windows services
  399. ----------------
  400. .. code-block:: python
  401. >>> list(psutil.win_service_iter())
  402. [<WindowsService(name='AeLookupSvc', display_name='Application Experience') at 38850096>,
  403. <WindowsService(name='ALG', display_name='Application Layer Gateway Service') at 38850128>,
  404. <WindowsService(name='APNMCP', display_name='Ask Update Service') at 38850160>,
  405. <WindowsService(name='AppIDSvc', display_name='Application Identity') at 38850192>,
  406. ...]
  407. >>> s = psutil.win_service_get('alg')
  408. >>> s.as_dict()
  409. {'binpath': 'C:\\Windows\\System32\\alg.exe',
  410. 'description': 'Provides support for 3rd party protocol plug-ins for Internet Connection Sharing',
  411. 'display_name': 'Application Layer Gateway Service',
  412. 'name': 'alg',
  413. 'pid': None,
  414. 'start_type': 'manual',
  415. 'status': 'stopped',
  416. 'username': 'NT AUTHORITY\\LocalService'}
  417. Projects using psutil
  418. =====================
  419. Here's some I find particularly interesting:
  420. - https://github.com/google/grr
  421. - https://github.com/facebook/osquery/
  422. - https://github.com/nicolargo/glances
  423. - https://github.com/Jahaja/psdash
  424. - https://github.com/ajenti/ajenti
  425. - https://github.com/home-assistant/home-assistant/
  426. Portings
  427. ========
  428. - Go: https://github.com/shirou/gopsutil
  429. - C: https://github.com/hamon-in/cpslib
  430. - Rust: https://github.com/rust-psutil/rust-psutil
  431. - Nim: https://github.com/johnscillieri/psutil-nim
  432. Security
  433. ========
  434. To report a security vulnerability, please use the `Tidelift security
  435. contact`_. Tidelift will coordinate the fix and disclosure.
  436. .. _`Giampaolo Rodola`: https://gmpy.dev/about
  437. .. _`donation`: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=A9ZS7PKKRM3S8
  438. .. _Tidelift security contact: https://tidelift.com/security
  439. .. _Tidelift Subscription: https://tidelift.com/subscription/pkg/pypi-psutil?utm_source=pypi-psutil&utm_medium=referral&utm_campaign=readme