0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Linuxでのシェル(コマンド)の連続実行

Posted at

Linuxでシェル(コマンド)を連続実行する方法を紹介します。

1. 環境

OS:CentOS Linux release 8.5.2111

OSバージョン
[root@centos85 work]# cat /etc/redhat-release
CentOS Linux release 8.5.2111
[root@centos85 work]

実際に動かして確認するために、以下のシェルを使用します。

test1.sh、test2.sh
#!/bin/bash

echo "$(basename "$0") を開始します"
echo "$(basename "$0") の処理中"

sleep 5

if [[ "$1" =~ ^[0-9]+$ ]]; then
    exit_code=$1
else
    exit_code=255
fi

echo "$(basename "$0") を終了します (exit_code=$exit_code)"
exit $exit_code

このシェルは終了時に引数の値でexitします。

2. シェル(コマンド)の連続実行

2.1. 順番に全部実行する(;

command1 ; command
前のコマンドが終了すると、結果に関わらず次のコマンドが実行されます。

実行結果(1つめのシェルが正常終了)
[root@centos85 work]# ./test1.sh 0 ; ./test2.sh 0
test1.sh を開始します
test1.sh の処理中
test1.sh を終了します (exit_code=0)
test2.sh を開始します
test2.sh の処理中
test2.sh を終了します (exit_code=0)
[root@centos85 work]#
実行結果(1つめのシェルが正常終了でない)
[root@centos85 work]# ./test1.sh 1 ; ./test2.sh 0
test1.sh を開始します
test1.sh の処理中
test1.sh を終了します (exit_code=1)
test2.sh を開始します
test2.sh の処理中
test2.sh を終了します (exit_code=0)
[root@centos85 work]#

2.2. 前のコマンドが成功したら次を実行(&&

command1 && command2
前のコマンドが正常終了(exit 0)した場合に次のコマンドを実行されます。
前のコマンドが正常終了(exit 0)でない場合には次のコマンドは実行されません。

実行結果(1つめのシェルが正常終了)
[root@centos85 work]# ./test1.sh 0 && ./test2.sh 0
test1.sh を開始します
test1.sh の処理中
test1.sh を終了します (exit_code=0)
test2.sh を開始します
test2.sh の処理中
test2.sh を終了します (exit_code=0)
[root@centos85 work]#
実行結果(1つめのシェルが正常終了でない)
[root@centos85 work]# ./test1.sh 1 && ./test2.sh 0
test1.sh を開始します
test1.sh の処理中
test1.sh を終了します (exit_code=1)
[root@centos85 work]#

2.3. 前のコマンドが失敗したら次を実行(||

command1 || command2
前のコマンドが正常終了(exit 0)でない場合に次のコマンドを実行されます。
前のコマンドが正常終了(exit 0)した場合には次のコマンドは実行されません。

実行結果(1つめのシェルが正常終了)
[root@centos85 work]# ./test1.sh 0 || ./test2.sh 0
test1.sh を開始します
test1.sh の処理中
test1.sh を終了します (exit_code=0)
[root@centos85 work]#
実行結果(1つめのシェルが正常終了でない)
[root@centos85 work]# ./test1.sh 1 || ./test2.sh 0
test1.sh を開始します
test1.sh の処理中
test1.sh を終了します (exit_code=1)
test2.sh を開始します
test2.sh の処理中
test2.sh を終了します (exit_code=0)
[root@centos85 work]#

3. シェル(コマンド)の連続実行をログファイルに出力

シェル(コマンド)の連続実行をログファイルに出力するには以下のように、サブシェルまたはbash -cコマンドで記述します。

( command1 ; command2 ) > logfile 2>&1
または
bash -c 'command1 ; command2' > logfile 2>&1

実行結果
[root@centos85 work]# ( ./test1.sh 0 ; ./test2.sh 0 ) > logfile 2>&1
[root@centos85 work]# cat logfile
test1.sh を開始します
test1.sh の処理中
test1.sh を終了します (exit_code=0)
test2.sh を開始します
test2.sh の処理中
test2.sh を終了します (exit_code=0)
[root@centos85 work]#
実行結果
[root@centos85 work]# bash -c './test1.sh 0 ; ./test2.sh 0' > logfile 2>&1
[root@centos85 work]# cat logfile
test1.sh を開始します
test1.sh の処理中
test1.sh を終了します (exit_code=0)
test2.sh を開始します
test2.sh の処理中
test2.sh を終了します (exit_code=0)
[root@centos85 work]#

バックグラウンドで実行させたい場合は以下のように末尾に&を付与します。
( command1 ; command2 ) > logfile 2>&1 &
または
bash -c 'command1 ; command2' > logfile 2>&1 &

実行結果
[root@centos85 work]# ( ./test1.sh 0 ; ./test2.sh 0 ) > logfile 2>&1 &
[1] 1464
[root@centos85 work]#
[1]+  終了                  ( ./test1.sh 0; ./test2.sh 0 ) > logfile 2>&1
[root@centos85 work]# cat logfile
test1.sh を開始します
test1.sh の処理中
test1.sh を終了します (exit_code=0)
test2.sh を開始します
test2.sh の処理中
test2.sh を終了します (exit_code=0)
[root@centos85 work]#
実行結果
[root@centos85 work]# bash -c './test1.sh 0 ; ./test2.sh 0' > logfile 2>&1 &
[1] 1477
[root@centos85 work]#
[1]+  終了                  bash -c './test1.sh 0 ; ./test2.sh 0' > logfile 2>&1
[root@centos85 work]# cat logfile
test1.sh を開始します
test1.sh の処理中
test1.sh を終了します (exit_code=0)
test2.sh を開始します
test2.sh の処理中
test2.sh を終了します (exit_code=0)
[root@centos85 work]#

nohupを使用する場合はbash -cコマンドで以下のように記述します。
nohup bash -c 'command1 ; command2' < /dev/null > logfile 2>&1 &

実行結果
[root@centos85 work]# nohup bash -c './test1.sh 0 ; ./test2.sh 0' < /dev/null >
logfile 2>&1 &
[1] 1489
[root@centos85 work]#
[1]+  終了                  nohup bash -c './test1.sh 0 ; ./test2.sh 0' < /dev/null > logfile 2>&1
[root@centos85 work]# cat logfile
test1.sh を開始します
test1.sh の処理中
test1.sh を終了します (exit_code=0)
test2.sh を開始します
test2.sh の処理中
test2.sh を終了します (exit_code=0)
[root@centos85 work]#

以上

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?