在 Python 中从进程 ID 中获取父进程 ID 和子进程 ID

Use*_*007 5 python solaris process rhel

我正在尝试获得ppid我想要的过程。

我使用以下代码来获取pid

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

现在在str[0],我有进程的pid说ruby,我想获取ppid同一进程的父进程ID和子进程ID。

我需要在Solaris as well as Red Hat Enterprise Linux6.0上运行此解决方案

有什么办法可以得到喜欢getppid()getchildid()吗?还是我需要grep再次通过命令进行拆分?

Aro*_*vit 7

使用此代码是一个坏主意。您的代码在 solaris 上不起作用。您可以使用“psutil”库,这样您就可以使您的代码独立于操作系统。 https://github.com/giampaolo/psutil

p = psutil.Process(7055)
parent_pid = p.ppid()
Run Code Online (Sandbox Code Playgroud)