diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2015-01-03 15:02:12 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2015-01-03 15:02:48 +0100 |
commit | f3d8137d8b79cd715b3a36e1408b04a0fb599dd1 (patch) | |
tree | 6c544ddd4511d472d05fb8643a1aa5d4d911de44 | |
parent | c8a3f82e3810b6fc5fe66ff2fae66062ddc1b539 (diff) | |
download | psutil-f3d8137d8b79cd715b3a36e1408b04a0fb599dd1.tar.gz |
Issue #568 pidof.py: also consider process name()
-rw-r--r-- | CREDITS | 5 | ||||
-rw-r--r-- | HISTORY.rst | 1 | ||||
-rw-r--r-- | TODO | 2 | ||||
-rwxr-xr-x | examples/pidof.py | 27 |
4 files changed, 20 insertions, 15 deletions
@@ -272,4 +272,7 @@ I: 561 N: Sylvain Mouquet E: sylvain.mouquet@gmail.com -I: 565
\ No newline at end of file +I: 565 + +N: karthikrev +I: 568 diff --git a/HISTORY.rst b/HISTORY.rst index e74fbdb7..334c7623 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -9,6 +9,7 @@ Bug tracker at https://github.com/giampaolo/psutil/issues - #553: new examples/pstree.py script. - #564: C extension version mismatch in case the user messed up with psutil installation or with sys.path is now detected at import time. +- #568: New examples/pidof.py script. - #569: [FreeBSD] add support for process CPU affinity. **Bug fixes** @@ -39,8 +39,6 @@ LOWER PRIORITY * AIX support? - * examples/pidof.py (same as 'pidof' cli tool) - * examples/taskmgr-gui.py (using tk). * system-wide number of open file descriptors: diff --git a/examples/pidof.py b/examples/pidof.py index c8f52999..02c066af 100755 --- a/examples/pidof.py +++ b/examples/pidof.py @@ -7,7 +7,7 @@ """ A clone of 'pidof' cmdline utility. -$ pidof /usr/bin/python +$ pidof python 1140 1138 1136 1134 1133 1129 1127 1125 1121 1120 1119 """ @@ -16,28 +16,31 @@ import psutil import sys -def pidsof(pgm): +def pidof(pgname): pids = [] - # Iterate on all proccesses and find matching cmdline and get list of - # corresponding PIDs for proc in psutil.process_iter(): + # search for matches in the process name and cmdline + try: + name = proc.name() + except psutil.Error: + name = "" try: cmdline = proc.cmdline() - pid = proc.pid except psutil.Error: - continue - if len(cmdline) > 0 and cmdline[0] == pgm: - pids.append(str(pid)) + cmdline = [] + if name == pgname or cmdline and cmdline[0] == pgname: + pids.append(str(proc.pid)) return pids def main(): if len(sys.argv) != 2: - sys.exit('usage: %s pgm_name' % __file__) + sys.exit('usage: %s pgname' % __file__) else: - pgmname = sys.argv[1] - pids = pidsof(pgmname) - print(" ".join(pids)) + pgname = sys.argv[1] + pids = pidof(pgname) + if pids: + print(" ".join(pids)) if __name__ == '__main__': main() |