web-dev-qa-db-ja.com

アレイを5つのファイルのセットに分割し、それらを並行してダウンロードするにはどうすればよいですか?

シェルスクリプトをtestMachineBで実行しているときに、ファイルをtestMachineCtestMachineAからtestMachineAにコピーしようとしています。

ファイルがtestMachineBにない場合は、確かにtestMachineCにあるはずです。したがって、最初にtestMachineBからファイルをコピーしようとします。ファイルがtestMachineBにない場合は、testMachineCに移動して同じファイルをコピーします。

PARTITIONSは、ディレクトリFOLDER_LOCATIONtestMachineAにコピーする必要のあるファイルパーティション番号です。

#!/bin/bash

readonly FOLDER_LOCATION=/export/home/username/pooking/primary
readonly MACHINES=(testMachineB testMachineC)
PARTITIONS=(0 3 5 7 9 11 13 15 17 19 21 23 25 27 29) # this will have more file numbers around 400

dir1=/data/snapshot/20140317

# delete all the files first
find "$FOLDER_LOCATION" -mindepth 1 -delete
for el in "${PARTITIONS[@]}"
do
    scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 username@${MACHINES[0]}:$dir1/s5_daily_1980_"$el"_200003_5.data $FOLDER_LOCATION/. || scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 username@${MACHINES[1]}:$dir1/s5_daily_1980_"$el"_200003_5.data $FOLDER_LOCATION/.
done

問題の説明:-

今私がやろうとしているのは、パーティション番号を含むPARTITIONS配列を5つのファイルのセットに分割することです。そこで、5つのファイルが並列にある最初のセットをコピーします。これらの5つのファイルが完了したら、次のセットに移動します。このセットにはさらに5つのファイルがあり、それらを再度並行してダウンロードし、すべてのファイルが完了するまでこれを続けます。

すべてのファイルを並行してダウンロードするのではなく、一度に5つのファイルだけをダウンロードします。

これは、bashシェルスクリプトを使用して実行できますか?

更新:-

あなたが提案しているこのようなものは?

echo $$

readonly FOLDER_LOCATION=/export/home/username/pooking/primary
readonly MACHINES=(testMachineB testMachineC)
ELEMENTS=(0 3 5 7 9 11 13 15 17 19 21 23 25 27 29)
LEN_ELEMENTS=${#ELEMENTS[@]}
X=0

dir1=/data/snapshot/20140317

function download() {
    if [[ $X < $LEN_ELEMENTS ]]; then
        (scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 username@${MACHINES[0]}:$dir1/s5_daily_1980_"${ELEMENTS[$X]}"_200003_5.data $FOLDER_LOCATION/. || scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 username@${MACHINES[1]}:$dir1/s5_daily_1980_"${ELEMENTS[$X]}"_200003_5.data $FOLDER_LOCATION/.) && kill -SIGHUP $$ 2>/dev/null &
    fi
}

trap 'X=$((X+1)); download' SIGHUP

# delete old files
find "$FOLDER_LOCATION" -mindepth 1 -delete

# initial loop
for x in {1..5}
do
    download
done

# waiting loop
while [ $X -lt $LEN_ELEMENTS ]
do
    sleep 1
done

上記は正しく見えますか?また、削除コマンドはどこに配置すればよいですか?

2
arsenal

このようなもの:

# Your variable initialization
readonly FOLDER_LOCATION=/export/home/username/pooking/primary
readonly MACHINES=(testMachineB testMachineC)
PARTITIONS=(0 3 5 7 9 11 13 15 17 19 21 23 25 27 29) # this will have more file numbers around 400

dir1=/data/snapshot/20140317

# delete all the files first
find "$FOLDER_LOCATION" -mindepth 1 -delete

# Bash function to copy a single file based on your script
do_copy() {
  el=$1
  scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 david@${FILERS_LOCATION[0]}:$dir1/s5_daily_1980_"$el"_200003_5.data $PRIMARY/. || scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 david@${FILERS_LOCATION[1]}:$dir1/s5_daily_1980_"$el"_200003_5.data $PRIMARY/.
}

# export -f is needed so GNU Parallel can see the function
export -f do_copy

# Run 5 do_copy in parallel. When one finishes, start another.
# Give them each an argument from PRIMARY_PARTITION
parallel -j 5 do_copy ::: "${PRIMARY_PARTITION[@]}"

詳しく知ることができ:

  • 簡単な紹介については、紹介ビデオをご覧ください: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
  • チュートリアル(man parallel_tutorial)をウォークスルーします。あなたのコマンドラインはそれのためにあなたを愛します。

GNU並列の10秒のインストール:

(wget -O - pi.dk/3 || curl pi.dk/3/ || fetch -o - http://pi.dk/3) | bash
2
Ole Tange

トラッピングを試してください:

#!/bin/bash

echo $$

ELEMENTS=(0 1 2 3 4 5 6)
LEN_ELEMENTS=${#ELEMENTS[@]}
X=0

function download() {
    if [[ $X < $LEN_ELEMENTS ]]; then
        (echo ${ELEMENTS[$X]}) && kill -SIGHUP $$ &
        #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        # here put your code, important to and it with kill (whole command) 
        # and put & at the end to background this
    fi
}

trap 'X=$((X+1)); download' SIGHUP

# initial loop
for x in {1..5}
do
    X=$((X+1))
    download
done

# waiting loop
while [ $X -lt $LEN_ELEMENTS ]
do
    sleep 1
done

このソリューションは、子プロセスが作業を終了した後に親プロセスに返送されるシグナルに基づいています。この場合、SIGHUPシグナルを使用し、次のように宣言します。

trap download SIGHUP

つまり、SIGHUPを取得した場合は、downloadを実行します。 kill -SIGHUP <script_pid>を実行して、手動でバンプすることができます。その後、スクリプトはwhileループで子プロセスからのシグナルを待機しています。

注意

このソリューションは、2つの状態の値であるシグナルに基づいています。つまり、多数のプロセスが同時にトラップする場合(トラップ間の時間はトラップのサービス時間よりも短くなります)、最初のプロセスのみが実行されます。

0
fazie

あまり洗練されていないソリューションの場合は、それぞれ5つの要素の配列スライスを抽出し、バックグラウンドでscpコマンドを起動するだけです。

投稿したものと同じ基本的なスクリプトを使用します(ただし、おそらくタイプミスであるため、scpコマンドの最後にある末尾の.を削除します)。

#!/bin/bash

#readonly FOLDER_LOCATION=/export/home/username/pooking/primary
readonly FOLDER_LOCATION=/home/terdon/foo/bar
#readonly MACHINES=(testMachineB testMachineC)
readonly MACHINES=(badabing)
PARTITIONS=(0 3 5 7 9 11 13 15 17 19 21 23 25 27 29) # this will have more file numbers around 400

dir1=/data/snapshot/20140317

# delete all the files first
find "$FOLDER_LOCATION" -mindepth 1 -delete

## ${#PARTITIONS[@]} is the number of elements in the arrya.
## Therefore, $lim is set to that number divided by 5. This
## is used later to split the array into five sets of files.
lim=$((${#PARTITIONS[@]}/5))


## The seq command will print the numbers from 0 to
## the length of the array, incrementing by $lim
for i in $(seq 0 $lim ${#PARTITIONS[@]})
do
    ## This will just print the $lim elements of the array starting
    ## from the current value of $i. The sed will convert spaces to commas
    ## so that it is the right format for brace expansion.
    files=$(echo "${PARTITIONS[@]:$i:$lim}" | sed 's/ /,/g')

    ## This launches the scp commands. {"$files"} will expand to the current 
    ## array slice. For example 0,1,2,3. Since this is run in the background,
    ## it will immediately continue on to the next slice. The result will be
    ## 5 scp processes running in parallel, each downloading one file.
    scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 username@${MACHINES[0]}:$dir1/s5_daily_1980_{"$files"}_200003_5.data $FOLDER_LOCATION/ || scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 username@${MACHINES[1]}:$dir1/s5_daily_1980_{"$files"}_200003_5.data $FOLDER_LOCATION/ &
done

警告:配列の長さが5で完全に割り切れない場合、ストラングラーを取得するために6番目のscpプロセスが起動される可能性があります。例えば:

ARRAY=( $(seq 0 403) )
lim=$((${#ARRAY[@]}/5))
for i in $(seq 0 $lim ${#PARTITIONS[@]}); do echo ${PARTITIONS[@]:$i:$lim}; done

上記は6行の出力を生成します:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
400 401
0
terdon