LoginSignup
8
5

More than 5 years have passed since last update.

同じコマンドを複数並列実行する便利シェル関数

Last updated at Posted at 2017-08-15

小ネタ。

parallel () {
  if [ $# -lt 2 ]; then
    echo -e "Usage:\n  parallel <N> <command>"
    return 1
  fi
  seq $1 | xargs -I{} -P $@
}

.bashrc とか .zshrc とかにこれを書いておくと、

$ parallel 3 echo foo
foo
foo
foo

こんな感じで parallel <並列数> <実行コマンド> で同じコマンドを並列実行できて便利。

$ parallel 3 echo process_{}
process_1
process_3
process_2

さらに、実行コマンド内の {}seq が生成したユニークな番号に置き換えられるので、これも何かに使えるかもしれない (並列実行なので順序はバラバラ)

[追記] GNU Parallel

このエントリを書いた当時は知らなかったけど、GNU Parallel という便利コマンドがあってこちらの方が高性能。

8
5
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
8
5