スクリプトを並列に実行すると、Bash スクリプトがスリープ状態になりません。

スクリプトを並列に実行すると、Bash スクリプトがスリープ状態になりません。

並列に実行する必要があるいくつかの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配置も不要です

おすすめ記事