2

I have a Python 3.9 script that starts another process in a new console. The 'other process' keeps running even after the original one has completed.

This is what I have on Windows:

# startup.py script
# =================
import sys,  subprocess

if __name__ == '__main__':
    print('start startup.py script')
    arguments = ['python', 'other_process.py']
    arguments.extend(sys.argv[1:])
    subprocess.Popen(
        arguments,
        creationflags = subprocess.CREATE_NEW_CONSOLE,
    )
    print('end startup.py script')

It works great. Below you see the original console on the left, in which I invoke startup.py. I also pass it a --help flag, which is then simply passed to the other_process.py script. On the right, you see the other_process.py script running. Please note that the original startup.py script has already finished, while the other_process.py script is still running. That's exactly what I need:

enter image description here

The subprocess.CREATE_NEW_CONSOLE parameter doesn't work on Linux. I've heard that setting shell=True would have a similar effect, but it doesn't spawn a new console.

How can I get the same effect on Linux?

K.Mulier
  • 8,069
  • 15
  • 79
  • 141

1 Answers1

1

Unix doesn’t provide this option/service, but you can run a terminal emulator:

subprocess.Popen(["gnome-terminal","--"]+arguments)

There isn’t a standard means of finding which terminal emulator to use (or even which are available), unfortunately. Checking shutil.which for a few common ones might be the right idea; from Wikipedia’s list, I’d recommend gnome-terminal, konsole, and xterm. You still then have to deal with the slightly different syntax to run a command in each.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
  • Thank you @Davis Herring. I'm only a sporadic Linux user. Can you elaborate on which "common ones" you'd suggest to look for? – K.Mulier Oct 24 '22 at 16:42