I want to execute 2 python programs(both run flask servers on different ports) in 2 different terminal windows, from a single bash script.
This is my current bash script,
So when I say,
./runner.sh It should execute
b.py in 2 separate terminal windows.
You can launch a terminal via a command; this should include an option to execute a command inside the terminal as well.
There are various GUI terminal applications available on GNU/Linux. They are usually paired with the DE (desktop environment). The default DE on Raspbian (“the DE formerly known as PIXEL”, for lack of a better name1) is derived from LXDE, whose terminal app is
lxterminal. You can read the documentation for that with
man lxterminal. The execute option is
lxterminal -e "/usr/bin/python /home/pi/myscript.py"
Using quotes here is required if the command contains whitespace. Beware that the terminal will close when
myscript.py ends, which can make it hard to determine what went wrong if it doesn’t work properly.
To get around that what should work (I’m not an LXDE user, but I tested it with another terminal app) is to wrap it in a shell script:
#!/bin/sh /usr/bin/python /home/pi/myscript.py $SHELL
This will open a shell (ie., your normal command line) after
myscript.py exits. So to use this,
lxterminal -e "/path/to/that/script". The script must be executable (
chmod a+x whatever.sh).
- Why they decided the DE should not have a name I don’t know. It seems the opposite of user friendly, but perhaps that depends which way your head is on.
b.py should have “hash-bang”-lines at the top, to tell the environment how to execute them. They look similar to:
There is a good chance they are already there, but check to make sure.
Once that is done, your
runner.sh, placed and executed in the same directory, should look like so:
#!/bin/sh lxterminal -e ./a.py & lxterminal -e ./b.py &
& of the
lxterminal-lines makes sure that this process is executed in the background, and that the script continues to execute.