3

When running a secondary python script:

  • Is it possible to run a subprocess.Popen, or subprocess.call or even execfile in a new terminal? (as in simply a different terminal than the current terminal where the script is run).
  • Alternatively, if before running my program (main), I open two terminals first, can I then point the secondary script to the second terminal? (so somehow getting the ID of open terminals, and then using a specific one among them, to perform the subprocess).

An example, two subprocesses to be run, first.py should be called first, only then the second is called, second.py. Because the two scripts first.py and second.py are interdependent (as in first.py goes to wait mode, until second.py is run, then first.py resumes, and I don't know how to make this communication work between them in terms of subprocesses.)

import subprocess

command = ["python", "first.py"]
command2 = ["python", "second.py"]
n = 5
for i in range(n):
    p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    p2 = subprocess.Popen(command2, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    while True:
        output = p.stdout.readline().strip()
        print output
        if output == 'stop':
            print 'success'
            p.terminate()
            p2.terminate()
            break

Framework (Ubuntu, python 2.7)

  • 3
    What do you mean by 'in a new terminal'? What are you trying to do? –  Apr 14 '16 at 13:09
  • 1
    The question you link to says nothing about terminal windows. What are you trying to do? –  Apr 14 '16 at 13:11
  • @DisplayName I'm trying something like `subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,creationflags=subprocess.CREATE_NEW_CONSOLE)` where `command=["python", "test.py"]` but I get "'module' object has no attribute 'CREATE_NEW_CONSOLE' ". Apparently CREATE_NEW_CONSOLE only works for windows –  Apr 14 '16 at 13:19
  • Please [edit] your question and add the code there. –  Apr 14 '16 at 14:08
  • Related: [How can I open two consoles from a single script](http://stackoverflow.com/q/19479504/4279) (consider providing GUI instead or look at the code with `x-terminal-emulator`) – jfs Apr 14 '16 at 18:13
  • Related: [Execute terminal command from python in new terminal window?](http://stackoverflow.com/q/19308415/4279) – jfs Apr 14 '16 at 18:22

3 Answers3

2

If you're using tmux, you can specify which target you want the command to run in:

tmux send -t foo.0 ls ENTER

So, if you've created a tmux session foo.0, you should be able to do:

my_command = 'ls'
tmux_cmd = ['tmux', 'send', '-t', 'foo.0', my_command]
p = subprocess.Popen(tmux_cmd)
fredrik
  • 9,631
  • 16
  • 72
  • 132
  • Thanks for your suggestions, unfortunately I m not using tmux. Any alternatives come to mind? CREATE_NEW_CONSOLE is not an option sadly as that's only for windows machines –  Apr 14 '16 at 13:35
2

I guess you want something like

subprocess.call(['xterm','-e','python',script])

Good old xterm has almost no frills; on a Freedesktop system, maybe run xdg-terminal instead. On Debian, try x-terminal-emulator.

However, making your program require X11 is in most cases a mistake. A better solution is to run the subprocesses with output to a log file (or a socket, or whatever) and then separately run tail -f on those files (in a different terminal, or from a different server over ssh, or with output to a logger which supports rsyslog, or or or ...) which keeps your program simple and modular, free from "convenience" dependencies.

Community
  • 1
  • 1
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thanks a lot, I think I m very close with your suggestion, great stuff. Both terminals open and the scripts start running, but how can I now monitor their outputs, `output = p.stdout.readline().strip()` does not seem to work anymore, since now they are in separate terminals –  Apr 14 '16 at 15:52
  • A more useful approach might be to start a simple program to receive and print the output from your main program in a separate terminal, or even just log to a file, and run a regular `tail -f` on the log file in a separate terminal. – tripleee Apr 14 '16 at 16:58
  • oh I see, sounds reasonable! How do I receive the output from subprocess p now that it's in xterm? –  Apr 14 '16 at 17:01
  • Don't run the subprocess in an `xterm`. – tripleee Apr 14 '16 at 17:11
  • The point is to make this work with xterm... As i wanted the subprocesses to be in different terminals. –  Apr 14 '16 at 17:51
  • @user929304 you can check [this](http://stackoverflow.com/questions/36596354/how-to-get-exit-code-from-subprocess-popen) which is how you can filter the output from `subprocess.Popen`. – fredrik Apr 14 '16 at 20:46
  • Once you run your subprocess in a terminal, I don't think there is a way to capture its output. Updated this answer with a suggestion for a different approach. – tripleee Apr 15 '16 at 04:18
1

You can specify the tty of the terminal window you wish the command to be carried out in:

ls > /dev/ttys004

However, I would recommend going for the tmux approach for greater control (see my other answer).

fredrik
  • 9,631
  • 16
  • 72
  • 132