Skip to content
Advertisement

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 🙂

Advertisement

Answer

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).

There are three processes here: shell=True causes /bin/sh to run that runs gnome-terminal that runs python (perhaps indirectly). gnome-terminal may be just a starter program that delegates starting python to a running server process and exits.

shell=False won’t help here. You’ll get gnome-terminal’s pid instead of /bin/sh—you won’t get python process’ pid.

You could write the pid in a known file or communicate it via pipe/socket.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement