LoginSignup
21
21

More than 5 years have passed since last update.

xargs でシェルスクリプトの並列実行

Last updated at Posted at 2013-11-07

仕事でちょっと必要になったので。

単にバックグラウンド処理するだけでなく、同時実行数を制御したかった。

昔のメモが調べてみたらすぐ見つかったので、Qiitaに転記しときます。(http://d.hatena.ne.jp/ux00ff/20120730/1343655384)

# 別のスクリプトの出力を処理する
pre_process.sh | xargs -n 1 -P 2 ./post_process.sh

# ファイルから読み込む
cat target_list.txt | xargs -n 1 -P 2 ./process.sh

# 決まったデータを順に処理する
(for DATA in a b c d e f; do echo $DATA ;done) | xargs -n 1 -P 2 ./parallel.sh

普通に使うと関数が使えなかったり、大きなシェルスクリプトの一部で使う分にはちょっぴり不便だけど、昔ながらのパイプでつなぐスタイルで書く分にはとても便利。

関数を無理矢理渡す方法が、

にあった。export -f で関数を環境に突っ込んで、bash -c で実行する。

21
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
21