Search code examples
pythonlinuxbashcommand-linegnome-terminal

Is there a way to wait (in a bash script) for a python program (started in a new terminal) to end before executing the next command?


To automate simulations I created a single bash script (see below) running 3 python programs in parallel, interconnected with sockets, each in a new terminal (The behavior of the software requires the 3 subprograms to be ran in separate terminals).

#!/bin/bash
    
gnome-terminal -- ./orchestrator.py -c config/orchestrator/configWorstCaseRandomV.yml -v
gnome-terminal -- ./server.py -c config/server/config.yml -s config/server/systems/ault14mix.yml --simulate -v
gnome-terminal -- ./simulation/traces/simulate_traces.py -m September

What I want to do is to re-execute the same 3 pythons programs but with different parameters ONLY after the 3 previous programs have ended. But here the 3 programs are started at the same time and I have no way of knowing when then end in my bash script.

I tried to simply add the corresponding commands at the end of my script, but logically it doesn't wait for the previous simulation to be completed to start the next 3 programs.

So my question is, is there a way of knowing when a program is ended in another terminal before executing the next lines of a bash script ?


Solution

  • I assume gnome-terminal is used only to run programs in parallel, and without it, each Python script would run in the foreground.

    In this case you may run them in background using the ampersand character & (instead of gnome-terminal) and get their process ID with $! and finally wait for them with the wait command, e.g.:

    #!/bin/bash
        
    ./orchestrator.py -c config/orchestrator/configWorstCaseRandomV.yml -v &
    pid1=$!
    ./server.py -c config/server/config.yml -s config/server/systems/ault14mix.yml --simulate -v &
    pid2=$!
    ./simulation/traces/simulate_traces.py -m September &
    pid3=$!
    wait $pid1 $pid2 $pid3