4

I have a problem where I need to write a python script, which basically opens one terminal windows and starts a node js server in it, then opens another terminal windows and starts a java program in it.

If I run two subprocess.call() functions they operate in the same terminal window.

Is there a way I can do this?

Thanks.:)

1 Answer 1

3

Use subprocess.Popen:

This will create new window for each bot and run program in it. The -i option for python3 is to make the window interactive after the TestBot3.py script finishes.

from subprocess import Popen, PIPE

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

Or you can use from subprocess import call

call(["python3", "TestBot1.py"])
call(["python3", "TestBot2.py"])
call(["python3", "TestBot3.py"])

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 TestBot1.py"])
call(['gnome-terminal', '-e', "python3 TestBot2.py"])
call(['gnome-terminal', '-e', "python3 TestBot3.py"])
Sign up to request clarification or add additional context in comments.

2 Comments

So if I want to use the Popen option and those two commands I want to execute are for example "cd Desktop && npm start" and "cd Desktop/folder && mvn exec:exec" , how do I achieve this? I am not very experienced in this manner so it is not that clear to me :) thanks
you can specify path of program file like "TestBot1.py" with you directory "Desktop/test/TestBot1.py".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.