-1

I want to write a shell script which can start two python scripts parallelly. Other commands in shell script will run after the two parallel python processes are done.

In conclusion, two key-points:

  1. run the two python processes parallelly;
  2. run other commands after the python processes are done.

How should I do that?

Ink
  • 845
  • 1
  • 13
  • 31

2 Answers2

3
declare -a pids
launch_first_program &
pids+=($!)
launch_second_program &
pids+=($!)
wait "${pids[@]}"
continue_with_our_day

After a process is launched in background with &, the variable $! will contain the child's PID. We can collect it into an array variable, then use wait to block till all of the listed processes are done.

Amadan
  • 191,408
  • 23
  • 240
  • 301
0

You can use wait in bash to wait for background processes to finish

python script1.py & python script2.py & wait Other commands

tomgalpin
  • 1,943
  • 1
  • 4
  • 12
  • 1
    A bare `wait` would wait for all background processes, including e.g. backgrounded `vim` (something that I'll pretty much always have in my environment). It may be it will work, but I'd rather be safe. :) – Amadan Sep 26 '19 at 09:42
  • @Amadan, yes agreed, your solution is far more elegant. Tom – tomgalpin Sep 26 '19 at 09:54