Bash script not sleeping when running scripts in parallel
Clash 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?
bash sleep
add a comment |Â
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?
bash sleep
2
You put your command in background with trailing&
, of course not wait for execute next instruction.
â æÂÂæÂÂçÂÂ
Apr 27 at 16:31
add a comment |Â
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?
bash sleep
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?
bash sleep
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
add a comment |Â
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
add a comment |Â
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
Perfect. Of course it was something simple!
â Micheal Simpson
Apr 27 at 17:12
add a comment |Â
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
Perfect. Of course it was something simple!
â Micheal Simpson
Apr 27 at 17:12
add a comment |Â
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
Perfect. Of course it was something simple!
â Micheal Simpson
Apr 27 at 17:12
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
You put your command in background with trailing
&
, of course not wait for execute next instruction.â æÂÂæÂÂçÂÂ
Apr 27 at 16:31