1

I have several bash scripts that I need to run in parallel. However, they are memory hogs so I would like to stagger each of them by 30 seconds but still run in parallel. For example:

hourr=($(seq 0 1 23))

for q in "${hourr[@]}";
do;
echo $q; sleep 10;
done

This waits 10 seconds and then outputs a number, sequentially, from 0 to 23. However, when I try to do this with my scripts:

hourr=($(seq 0 1 23))
input1="20160101"; 
input2="10"; #(Note: These are inputs to each of the scripts I want to run)
scriptdir="/path/to/somewhere"
for q in "${hourr[@]}"
do
if [ "${#q}" == "1" ]
then
hh=0${q}
else
hh=${q}
fi
echo $hh
( bash $scriptdir/Hour$hh.csh $input1 $input2 ; sleep 30 ) &
done
wait
echo "All done!"

However, all of the Hour scripts run at once (correctly, and in parallel) when the main script is executed, and does not wait the 30 seconds that I have specified to run one after the other. Any thoughts?

  • 2
    You put your command in background with trailing &, of course not wait for execute next instruction. – 林果皞 Apr 27 '18 at 16:31
1

What if you did it like this?

#/bin/bash

input1='20160101'
input2='10' #(Note: These are inputs to each of the scripts I want to run)
scriptdir='/path/to/somewhere'
for q in {00..23}
do
    hh="${q}"
    echo "$hh"
    ( bash "$scriptdir/Hour$hh.csh" "$input1" "$input2" ) &
    sleep 30
done
wait
echo "All done!"

As has been pointed out in the comments, the & is causing your sleep to also be executed in the background and therefore your script will not wait for it to finish before moving to the next iteration of the loop. Also your hourr array is unnecessary

  • Perfect. Of course it was something simple! – Micheal Simpson Apr 27 '18 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.