我有几个bash脚本需要并行运行。但是,它们是内存猪,因此我想将它们各自错开30秒,但仍可以并行运行。例如:
hourr=($(seq 0 1 23))
for q in "${hourr[@]}";
do;
echo $q; sleep 10;
done
这将等待10秒,然后依次输出一个从0到23的数字。但是,当我尝试使用脚本执行此操作时:
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!"
但是,在执行主脚本时,所有的Hour脚本会一次(正确且并行)运行,而不会等待我指定的30秒一个接一个地运行。有什么想法吗?
如果您这样做就怎么办?
#/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!"
正如注释中所指出的那样,&
也会导致您sleep
也在后台执行,因此您的脚本将不会等待它完成才移至循环的下一个迭代。而且你的hourr
数组是不必要的
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句