How to get the running process’ parent process’ ID in Python?
In Python, you can get the parent process’ pid by calling os.getppid()
:
import os
ppid = os.getppid()
One example:
The shell’s pid:
$ echo $$
21779
Start a python REPL and get its parent pid (the shell’s):
$ python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.getppid()
21779
>>>