Created
August 12, 2015 20:07
-
-
Save altaurog/65f9c0e9a8d016cff9b3 to your computer and use it in GitHub Desktop.
Get PID of n'th parent (linux only)
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
import os | |
""" | |
Get PID of n'th parent | |
This works in linux only | |
""" | |
def pnid(n=1, pid=os.getpid()): | |
for i in range(n): | |
try: | |
procfile = open("/proc/%s/status" % pid) | |
except IOError: | |
return -1 | |
with procfile as f: | |
for line in f: | |
if line.startswith("PPid:"): | |
pid = line.split()[1] | |
break | |
else: | |
pid = 0 | |
break | |
return int(pid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment