Bash script not sleeping when running scripts in parallel

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












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?







share|improve this question

















  • 2




    You put your command in background with trailing &, of course not wait for execute next instruction.
    – æž—果皞
    Apr 27 at 16:31















up vote
1
down vote

favorite












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?







share|improve this question

















  • 2




    You put your command in background with trailing &, of course not wait for execute next instruction.
    – æž—果皞
    Apr 27 at 16:31













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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?







share|improve this question













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?









share|improve this question












share|improve this question




share|improve this question








edited Apr 27 at 16:21
























asked Apr 27 at 16:15









Micheal Simpson

155




155







  • 2




    You put your command in background with trailing &, of course not wait for execute next instruction.
    – æž—果皞
    Apr 27 at 16:31













  • 2




    You put your command in background with trailing &, of course not wait for execute next instruction.
    – æž—果皞
    Apr 27 at 16:31








2




2




You put your command in background with trailing &, of course not wait for execute next instruction.
– æž—果皞
Apr 27 at 16:31





You put your command in background with trailing &, of course not wait for execute next instruction.
– æž—果皞
Apr 27 at 16:31











1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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






share|improve this answer





















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










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f440465%2fbash-script-not-sleeping-when-running-scripts-in-parallel%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










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






share|improve this answer





















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














up vote
1
down vote



accepted










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






share|improve this answer





















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












up vote
1
down vote



accepted







up vote
1
down vote



accepted






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






share|improve this answer













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







share|improve this answer













share|improve this answer



share|improve this answer











answered Apr 27 at 16:43









Jesse_b

10.3k22658




10.3k22658











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
















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















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




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












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f440465%2fbash-script-not-sleeping-when-running-scripts-in-parallel%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?