Unix & Linux: pausing a bash script until previous commands are finished (2 Solutions!!)
Channel: Roel Van de Paar
Date: 2020-08-13

Unix & Linux: pausing a bash script until previous commands are finished


The Question: I have a bash script that looks like the following:
##script
#!/bin/bash
rm data*
rm logfile*
for i in {1..30}
do
## append a & if you want to run it parallel;
nohup Rscript --vanilla main.R 10 100 $i &＞ logfile"$i" &
done
I would like to create another for loop after the first one to continue for
another 30. For example
##script
#!/bin/bash
rm data*
rm logfile*
for i in {1..30}
do
## append a & if you want to run it parallel;
nohup Rscript --vanilla main.R 10 100 $i &＞ logfile"$i" &

for i in {31..60}
do
## append a & if you want to run it parallel;
nohup Rscript --vanilla main.R 10 100 $i &＞ logfile"$i" &
done
I would like for the first set of jobs to finish before starting the new set.
But because of the nohup it seems that they are all run simultaneously.
I have nohup because I remotely login to my server and start the jobs there and
then close my bash. Is there an alternative solution?

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 25 people ==
You'll want to use the wait command to do this for you. You can either capture
all of the children process IDs and wait for them specifically, or if they are
the only background processes your script is creating, you can just call wait
without an argument. For example:
#!/bin/bash
# run two processes in the background and wait for them to finish

nohup sleep 3 &
nohup sleep 10 &

echo "This will wait until both are done"
date
wait
date
echo "Done"

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user ParanoidGeek (https://unix.stackexchange.com/users/185961), user Matei David (https://unix.stackexchange.com/users/111848), user masfenix (https://unix.stackexchange.com/users/92825), and the Stack Exchange Network (http://unix.stackexchange.com/questions/305039). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.