4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

シェルを同時並列実行する方法

Last updated at Posted at 2022-02-09

複数のシェルをバックグラウンドで同時並走させるときは以下のように
コマンドを&(アンパサンド)で繋ぐといいらしいです。
&はリスト演算子に含まれるようです。

$ sh shell_1.sh & sh shell_2.sh & sh shell_3.sh & sh shell_x.sh

もちもん上記のコマンドをshファイルの中に記述して実行することも可能です。

下記ファイルで検証してみました

shell_1.sh
#!/usr/bin/bash

DATETIME=`date +%Y%m%d_%H%M%S_%s`
echo "shell_1.sh start $DATETIME"

DATETIME=`date +%Y%m%d_%H%M%S_%s`
echo "shell_1.sh end $DATETIME"

shell_2.sh

#!/usr/bin/bash

DATETIME=`date +%Y%m%d_%H%M%S_%s`
echo "shell_2.sh start $DATETIME"

DATETIME=`date +%Y%m%d_%H%M%S_%s`
echo "shell_2.sh end $DATETIME"

shell_3.sh

#!/usr/bin/bash

DATETIME=`date +%Y%m%d_%H%M%S_%s`
echo "shell_3.sh start $DATETIME"

DATETIME=`date +%Y%m%d_%H%M%S_%s`
echo "shell_3.sh end $DATETIME"


douji.sh

#!/usr/bin/bash

sh shell_1.sh & sh shell_2.sh & sh shell_3.sh
$ sh douji.sh

出力結果

$ sh douji.sh
shell_2.sh start 20220209_232155_1644416515
shell_1.sh start 20220209_232155_1644416515
shell_3.sh start 20220209_232155_1644416515
shell_1.sh end 20220209_232155_1644416515
shell_2.sh end 20220209_232155_1644416515
shell_3.sh end 20220209_232155_1644416515

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?