Skip to content

Instantly share code, notes, and snippets.

@dnozay
Created June 12, 2017 20:49
Show Gist options
  • Save dnozay/7a463863c48ed1c2b39c19e30e08eaae to your computer and use it in GitHub Desktop.
Save dnozay/7a463863c48ed1c2b39c19e30e08eaae to your computer and use it in GitHub Desktop.
psutil: list process based on ppid
# 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