Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get process's grandparent id

How can i get process id of the current process's parent?
In general given a process id how can I get its parent process id?
e.g. os.getpid() can be used to get the proccess id, and os.getppid() for the parent, how do I get grandparent,

My target is linux(ubuntu) so platform specific answers are ok.

like image 294
Anurag Uniyal Avatar asked Nov 13 '09 10:11

Anurag Uniyal


People also ask

How do you get PID of grandparents?

"To get process's grandparent id" as OP asks, use psutil. Process(os. getppid()). ppid() (notice: getppid() , not getpid() ).

How do I find the parent ID of a process?

To determine the parent process of a specific process, we use the ps command. The output only contain the parent process ID itself. Using the output from the ps command we can determine the name of the process.

How do I find PID of all processes?

Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column. Click on any column name to sort.

How do you get parent process identification number in Linux?

In the GNU C Library implementation running on Linux, the process ID is the thread group ID of all threads in the process. You can get the process ID of a process by calling getpid . The function getppid returns the process ID of the parent of the current process (this is also known as the parent process ID).


2 Answers

By using psutil ( https://github.com/giampaolo/psutil ):

>>> import psutil
>>> psutil.Process().ppid()
2335
>>> psutil.Process().parent()
<psutil.Process (pid=2335, name='bash', cmdline='bash') at 140052120886608>
>>> 
like image 156
Giampaolo Rodolà Avatar answered Sep 19 '22 10:09

Giampaolo Rodolà


linux specific:

os.popen("ps -p %d -oppid=" % os.getppid()).read().strip()
like image 25
pixelbeat Avatar answered Sep 22 '22 10:09

pixelbeat