Add getpid.py.

This commit is contained in:
voussoir 2021-10-05 16:51:06 -07:00
parent 555e2fccb1
commit 1c1d517fd2
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

43
getpid.py Normal file
View 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:]))