Add getpid.py.
This commit is contained in:
parent
555e2fccb1
commit
1c1d517fd2
1 changed files with 43 additions and 0 deletions
43
getpid.py
Normal file
43
getpid.py
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
'''
|
||||||
|
getpid
|
||||||
|
======
|
||||||
|
|
||||||
|
Get PIDs for running processes that match the given process name.
|
||||||
|
|
||||||
|
Error level will be 0 if any processes are found, 1 if none are found.
|
||||||
|
|
||||||
|
> getpid process_name
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
> getpid python.exe
|
||||||
|
> getpid chrome.exe
|
||||||
|
'''
|
||||||
|
import argparse
|
||||||
|
import psutil
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from voussoirkit import betterhelp
|
||||||
|
from voussoirkit import vlogging
|
||||||
|
|
||||||
|
log = vlogging.getLogger(__name__, 'getpid')
|
||||||
|
|
||||||
|
def getpid_argparse(args):
|
||||||
|
status = 1
|
||||||
|
target = args.process_name.lower()
|
||||||
|
for process in psutil.process_iter():
|
||||||
|
if process.name().lower() == target:
|
||||||
|
print(process.pid)
|
||||||
|
status = 0
|
||||||
|
return status
|
||||||
|
|
||||||
|
@vlogging.main_decorator
|
||||||
|
def main(argv):
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
|
||||||
|
parser.add_argument('process_name')
|
||||||
|
parser.set_defaults(func=getpid_argparse)
|
||||||
|
|
||||||
|
return betterhelp.single_main(argv, parser, __doc__)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
raise SystemExit(main(sys.argv[1:]))
|
Loading…
Reference in a new issue