Unix & Linux: pausing a bash script until previous commands are finished (2 Solutions!!)

preview_player
Показать описание
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"

Рекомендации по теме
welcome to shbcf.ru