I don't think you can do this in a portable Python fashion. But there are two possibilities.
- The information is available from the
ps
command so you could analyze that.
- If you have a system with the
proc
file systems, you can open the file /proc/<pid>/status
and search for the line containing PPid:
, then do the same for that PID.
For example the following script will get you your PID, PPID and PPPID, permissions willing:
#!/bin/bash
pid=$$
ppid=$(grep PPid: /proc/${pid}/status | awk '{print $2'})
pppid=$(grep PPid: /proc/${ppid}/status | awk '{print $2'})
echo ${pid} ${ppid} ${pppid}
ps -f -p "${pid},${ppid},${pppid}"
produces:
3269 3160 3142
UID PID PPID C STIME TTY TIME CMD
pax 3142 2786 0 18:24 pts/1 00:00:00 bash
root 3160 3142 0 18:24 pts/1 00:00:00 bash
root 3269 3160 0 18:34 pts/1 00:00:00 /bin/bash ./getem.sh
Obviously, you'd have to open those files with Python.