Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To get Parent and ChildProcess ID from process ID in Python

I am trying to get the ppid of the process that I want.

I used following code to get the pid

proc=subprocess.Popen('ps -ae | grep ruby', shell=True, stdout=subprocess.PIPE, )
output=proc.communicate()[0]
str = output.split()

Now in the str[0], I have the pid of the process say ruby, I want to get the parent process ID ppid and child process ID of the same process.

I need this solution to be run on Solaris as well as Red Hat Enterprise Linux 6.0

Is there any way to get that like getppid() and getchildid()? Or do I need to do it by grep command again and splitting?

like image 412
User007 Avatar asked Nov 29 '13 17:11

User007


1 Answers

Using this code is a bad idea. Your code will not work on solaris. You can use 'psutil' library, that way you can keep your code independent of os. https://github.com/giampaolo/psutil

p = psutil.Process(7055)
parent_pid = p.ppid()
like image 66
Arovit Avatar answered Sep 23 '22 13:09

Arovit