Elie khalifeh Elie khalifeh - 1 year ago 55
Python Question

Get the status of a python process that was executed in another terminal

I have a python program that will create and execute another python script in a new terminal. To do so, I'm using subprocess.Popen. I'm trying to get the PID of the new process using .pid. However, the value of this pid doesn't seem to match the real pid of the newly created process (the two values don't match).

Here's a sample of my code:

from subprocess import Popen

p = Popen("gnome-terminal -e 'python'",shell = True)
print p.pid

In the newly opened terminal, I try to get the pid using:

import os
print os.getpid()

I'm getting two different values. Does anyone know how to get the correct pid? I need this to know when the second process has been completed in order to do an action in the first process.

Thanks for your help!

If you need more details, please let me know :)

Answer Source

According to the documentation.

If you set the shell argument to True, this is the process ID of the spawned shell.

So you'll need shell=False