Created
December 24, 2017 03:33
-
-
Save danbradham/2b00d7523740dceabf376c3ded30345c to your computer and use it in GitHub Desktop.
psutil get parent process
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
# -*- 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