3
votes
from subprocess import call
call(["python3", "/home/johngr/psdirc/TestBot1.py"]) and call(["python3", "/home/johngr/psdirc/TestBot2.py"]) and call(["python3", "/home/johngr/psdirc/TestBot3.py"])

The call is working but it only runs the first file. I want them all to run in their own terminal windows.

2
This question is unclear. Do you want to run them serially? At a time? What do you mean "in a own cmd"? - Charles Duffy
"in multiple cmds" -- cmd.exe is a shell; it has nothing to do with terminal windows. This question would be clearer if you said "in multiple windows", though you'd need to also specify your operating system, since any kind of dealing with windows is OS-specific. - Charles Duffy
I need them to run in multiple windows. - handsomebob10
@handsomebob10, any reason why you are changing your code? This most recent change invalidates one of the answers. - gunr2171
@handsomebob10, don't change the code in your question, the only thing you needed to do was add that you want each script run in it's own terminal window, that is the most important part of the question. - Padraic Cunningham

2 Answers

4
votes

Don't use and just run one after the other:

call(["python3", "/home/johngr/psdirc/TestBot1.py"])
call(["python3", "/home/johngr/psdirc/TestBot2.py"])
call(["python3", "/home/johngr/psdirc/TestBot3.py"])

If you don't want them to wait for the process to finish before starting the next use Popen:

 Popen(["python3", "/home/johngr/psdirc/TestBot1.py"])
 Popen(["python3", "/home/johngr/psdirc/TestBot2.py"])
 Popen(["python3", "/home/johngr/psdirc/TestBot3.py"])

call will Run the command described by args. Wait for command to complete, then return the returncode attribute. where Popen won't wait.

If you want to be sure each process exits with a non-zero exit status use check_call which will raise a CalledProcessError for any non-zero exit status.

To open a terminal for each you can use gnome-terminal with -e Execute the argument to this option inside the terminal:

call(['gnome-terminal', '-e', "python3 /home/johngr/psdirc/TestBot1.py"])
call(['gnome-terminal', '-e', "python3 /home/johngr/psdirc/TestBot2.py"])
call(['gnome-terminal', '-e', "python3 /home/johngr/psdirc/TestBot3.py"])

If you want to open tabs you can use --tab -e:

cmd =['gnome-terminal', '--tab', '-e', 'python3 /home/johngr/psdirc/TestBot1.py',
      '--tab', '-e','python3 /home/johngr/psdirc/TestBot2.py','--tab', '-e', 
      'python 3 /home/johngr/psdirc/TestBot3.py']
call(cmd)

You don't seem to have gnome-terminal so just replace it with lxterminal:

call(['lxterminal', '-e', 'python3 /home/johngr/psdirc/TestBot1.py'])

Not sure if --tab option is supported or not, I don't see any reference to it in the documentation.

1
votes

Answer to updated question

Use subprocess.Popen:

from subprocess import Popen, PIPE

bot1 = Popen(["lxterminal", "-e", "python3", "-i", "/home/johngr/psdirc/TestBot1.py"], stdout=PIPE, stderr=PIPE, stdin=PIPE)
bot2 = Popen(["lxterminal", "-e", "python3", "-i", "/home/johngr/psdirc/TestBot2.py"], stdout=PIPE, stderr=PIPE, stdin=PIPE)
bot3 = Popen(["lxterminal", "-e", "python3", "-i", "/home/johngr/psdirc/TestBot3.py"], stdout=PIPE, stderr=PIPE, stdin=PIPE)

This will put each into its own window. The -i option for python3 is to make the window interactive after the TestBot3.py script finishes. It doesn't hurt to have, even if you don't expect it to be interactive, so you can debug in case something does go wrong.

I did a test, and the new windows persist after exiting this script.

Answer to original question

I don't recommend this for production code, but:

not call(["python3", "/home/johngr/psdirc/TestBot1.py"]) \
    and not call(["python3", "/home/johngr/psdirc/TestBot2.py"]) \
    and not call(["python3", "/home/johngr/psdirc/TestBot3.py"])

Python treats the bash "Good" return code of 0 as False, and the and operator is lazy.

This of course presumes you expect each call to succeed, otherwise you will still not call all three. So you're much better off calling each on a separate line. Wrap it in a function if you want to clean up the code.