Last active
August 29, 2015 14:10
-
-
Save zed/09352b5d0dfd232d4bc8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Print info in json format about OS process specified by its pid. | |
Usage: procinfo PID | |
To install: | |
$ sudo apt-get install python-psutil # install dependencies | |
# https://pypi.python.org/pypi?:action=display&name=psutil#downloads | |
Example: | |
$ python procinfo.py 3451 | jq .cpu_percent,.memory_percent | |
0 | |
1.848394789177199 | |
""" | |
import json | |
import sys | |
import psutil # $ pip install psutil | |
try: | |
pid = int(sys.argv[1]) | |
except (ValueError, IndexError): | |
sys.exit(__doc__) # print usage info and exit | |
try: | |
procinfo = psutil.Process(pid).as_dict() | |
except (psutil.NoSuchProcess, psutil.AccessDenied): | |
sys.exit("can't get process info for %d pid" % pid) | |
else: | |
json.dump(procinfo, sys.stdout, indent=4, sort_keys=True, ensure_ascii=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment