0

I am trying to create a process that runs two separate bash scripts in separate terminal windows from a Python script that uses subprocess to call them. Currently I have something like this:

if <condition>:
    subprocess.Popen(["bash", "<file1>"]).wait()
    subprocess.Popen(["bash", "<file2>"]).wait()

I keep the wait calls at the end so that they don't overlap and I thought that since Popen() technically runs the calls separately they would run in different terminals. Is there any way to do this so that the two calls never interact or overlap in the same console?

Edit: The reason I need a new console for the second bash script is because the command called at the end of cannot be interrupted by new commands so whenever I have run this process manually I have always just opened a new terminal window to keep them separate. I realize there may be a better way of doing that but I am unaware of what it might be.

  • 1
    Does this answer your question? [subprocess.Popen in different console](https://stackoverflow.com/questions/15899798/subprocess-popen-in-different-console) – newby73 Jul 13 '23 at 19:34
  • Why the terminals and not just background? Are you expecting to interact with them? – Diego Torres Milano Jul 13 '23 at 19:44
  • That answer by @newby73 is outdated and I can't import that same creation flag from subprocess. And in response to Diego I am trying to have them in the background but the problem is one ofthe commands I need to run from my bash script cannot be interrupted by another command in the same terminal – William Addison Jul 17 '23 at 15:24
  • Programs inherit the file descriptors of their parent on UNIX, so it's 100% normal/expected for a program's output to go to the same terminal as its parent process. It's a Microsoft-ism to have anything else. – Charles Duffy Jul 17 '23 at 15:41
  • 1
    (Remember, most processes running on a typical Linux system _aren't attached to a terminal at all_; they might be reading and writing to/from FIFOs, pipes, sockets, or any number of other thing that aren't terminals. Moreover, most processes _aren't even attached to a GUI subsystem_, so there wouldn't be a place to create a window even if one wanted to). – Charles Duffy Jul 17 '23 at 15:43
  • 1
    So if you want to start a terminal, _you need to do that yourself_. See `xterm`, `gnome-terminal`, `rxvt`, and all the other terminals that exist; they can be invoked with `subprocess.Popen` just as anything else can be. – Charles Duffy Jul 17 '23 at 15:44
  • (What do you mean "cannot be interrupted by new commands"? It almost sounds like you might be better off running that in the background, f/e as a systemd user service, if your goal is to make sure it keeps running after your active script exits and to prevent the user's actions from interrupting it). – Charles Duffy Jul 18 '23 at 09:29
  • @CharlesDuffy it definitely would be preferred to have it run in the background but I already thought that Popen() would create a new, separate, process where it would run without interruptions so I suppose I'm wondering now is this even possible by calling any function from subprocess or should I use something else? – William Addison Jul 24 '23 at 14:00
  • `Popen` does create a new process, but that process inherits stdin, stdout, and stderr. You need to detach _all_ those file descriptors (or rather, reattach them to something that doesn't go away when the user closes the current TTY) for something to be in the background. – Charles Duffy Jul 24 '23 at 14:02

1 Answers1

-1

As shown by user1882644 in subprocess.Popen in different console :

To open in a different console, do (tested on Win7 / Python 3):

from subprocess import Popen, CREATE_NEW_CONSOLE

Popen('cmd', creationflags=CREATE_NEW_CONSOLE)

input('Enter to exit from Python script...')

Related How can I spawn new shells to run Python scripts from a base Python script?

newby73
  • 45
  • 11
  • 1
    This was the same post as above but that CREATE_NEW_CONSOLE flag must either be deprecated or has changed because I cannot import it and subprocess does not recognize it – William Addison Jul 17 '23 at 15:27
  • The OP is not on Windows, they're on Linux. This flag has _never_ been available on Linux, and it's not supposed to be. – Charles Duffy Jul 17 '23 at 15:42
  • This was tested on Windows 7 with Python 3. These very well may be prerequisites. – newby73 Jul 17 '23 at 17:14
  • Windows is _explicitly_ a prerequisite, as the relevant section of the subprocess documentation indicates, making this answer unsuited to the question it's attached to. – Charles Duffy Jul 18 '23 at 09:34