Created
June 12, 2017 20:49
-
-
Save dnozay/7a463863c48ed1c2b39c19e30e08eaae to your computer and use it in GitHub Desktop.
psutil: list process based on ppid
This file contains hidden or 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
# Rationale: find all subprocesses of a certain command and do something similar to os.killpg | |
# We need the process ids to wait and be more aggressive if they don't terminate in due time. | |
import psutil | |
def print_info(process): | |
with process.oneshot(): | |
print 'pid={0}, ppid={1}, cmd={2}'.format(process.pid, process.ppid(), str(process.cmdline())) | |
def process_info(pid): | |
pids = set() | |
try: | |
parent = psutil.Process(pid) | |
pids.add(parent.pid) | |
print_info(parent) | |
except psutil.NoSuchProcess: | |
return pids | |
for child in parent.children(recursive=True): | |
pids.add(child.pid) | |
print_info(child) | |
return pids |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment