user3830784
user3830784

Reputation: 355

How to start 2 programs that need separate terminals from a bash script?

I have 2 programs that I want to run, programA.py and programB.py. When I run them manually, I have to open separate terminals and type the following commands:

terminal1
python programA.py

terminal2
python programB.py

Each of these programs then output some data on the command line. Lastly, programA.py has to be fully started and waiting before programB.py can start (takes ~2s for programA.py to start and be ready to accept data).

If I am running these programs in Ubuntu, how can I write a bash script that accomplishes that? Right now, I have the following:

#!/bin/bash
python programA.py
python programB.py

This starts programA.py, but because programA.py then waits for input, programB.py doesn't start until you close out of programA.py. How can I change my script to run the two programs simultaneously?

Edit:

Using the advice given by Andreas Neumann below, changing the script to the following successfully launches the two programs:

#!/bin/bash
python programA.py &
sleep 5
python programB.py &

However, when both programs are launched, the code then doesn't work properly. Basically, programA.py is setting up a listening socket, and then creates an interface that the user works with. programB.py then starts afterwards, and runs a process, talking to programA.py over the sockets. When running the above script, programA starts, waits, programB starts, and then programA and B connect, form the interface, but then programB isn't running its background processes correctly.

Upvotes: 0

Views: 1455

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207415

Updated Answer

If you find my original answer below doesn't work, yet you still want to solve your question with a single script, you could do something like this:

#!/bin/bash
xterm -e "python ProgramA.py" &
sleep 5
python ProgramB.py

Original Answer

If programA is creating a user interface, you probably need that to be in the foreground, so start programB in the background:

{ sleep 5; python programB.py; } &
python ProgramA.py 

Upvotes: 1

Andreas Neumann
Andreas Neumann

Reputation: 10884

#!/bin/bash
python programA.py &
sleep 5 # give enough time to start
python programB.py &

Upvotes: 1

Related Questions