Skip to content

Instantly share code, notes, and snippets.

@altaurog
Created August 12, 2015 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save altaurog/65f9c0e9a8d016cff9b3 to your computer and use it in GitHub Desktop.
Save altaurog/65f9c0e9a8d016cff9b3 to your computer and use it in GitHub Desktop.
Get PID of n'th parent (linux only)
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