
並列に実行する必要があるいくつかの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秒間待たずに順番に実行されます。どんなアイデアがありますか?
答え1
それではどうなりますか?
#/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
配置も不要です