Skip to content

Instantly share code, notes, and snippets.

@danbradham
Created December 24, 2017 03:33
Show Gist options
  • Save danbradham/2b00d7523740dceabf376c3ded30345c to your computer and use it in GitHub Desktop.
Save danbradham/2b00d7523740dceabf376c3ded30345c to your computer and use it in GitHub Desktop.
psutil get parent process
# -*- coding: utf-8 -*-
import psutil
def get_parent_process(ok_names, limit=10):
'''Walk up the process tree until we find a process we like.
Arguments:
ok_names: Return the first one of these processes that we find
'''
depth = 0
this_proc = psutil.Process(os.getpid())
next_proc = parent = psutil.Process(this_proc.ppid())
while depth < limit:
if next_proc.name() in ok_names:
return next_proc.name()
next_proc = psutil.Process(next_proc.ppid())
depth += 1
return parent.name()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment