diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /python/psutil/examples/pidof.py | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'python/psutil/examples/pidof.py')
-rwxr-xr-x | python/psutil/examples/pidof.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/python/psutil/examples/pidof.py b/python/psutil/examples/pidof.py new file mode 100755 index 000000000..8692a3152 --- /dev/null +++ b/python/psutil/examples/pidof.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +# Copyright (c) 2009, Giampaolo Rodola', karthikrev. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + + +""" +A clone of 'pidof' cmdline utility. +$ pidof python +1140 1138 1136 1134 1133 1129 1127 1125 1121 1120 1119 +""" + +from __future__ import print_function +import psutil +import sys + + +def pidof(pgname): + pids = [] + for proc in psutil.process_iter(): + # search for matches in the process name and cmdline + try: + name = proc.name() + except psutil.Error: + pass + else: + if name == pgname: + pids.append(str(proc.pid)) + continue + + try: + cmdline = proc.cmdline() + except psutil.Error: + pass + else: + if cmdline and cmdline[0] == pgname: + pids.append(str(proc.pid)) + + return pids + + +def main(): + if len(sys.argv) != 2: + sys.exit('usage: %s pgname' % __file__) + else: + pgname = sys.argv[1] + pids = pidof(pgname) + if pids: + print(" ".join(pids)) + +if __name__ == '__main__': + main() |