Linuxでシェル(コマンド)を連続実行する方法を紹介します。
1. 環境
OS:CentOS Linux release 8.5.2111
[root@centos85 work]# cat /etc/redhat-release
CentOS Linux release 8.5.2111
[root@centos85 work]
実際に動かして確認するために、以下のシェルを使用します。
#!/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
前のコマンドが終了すると、結果に関わらず次のコマンドが実行されます。
[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]#
[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)でない場合には次のコマンドは実行されません。
[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]#
[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)した場合には次のコマンドは実行されません。
[root@centos85 work]# ./test1.sh 0 || ./test2.sh 0
test1.sh を開始します
test1.sh の処理中
test1.sh を終了します (exit_code=0)
[root@centos85 work]#
[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]#
以上