Last active
August 28, 2023 15:03
-
-
Save shoyan/6f45b6a005fd34a44264fa1dcb4d56e6 to your computer and use it in GitHub Desktop.
別プロセスの結果を取得するシェルスクリプトのサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
command1() { | |
echo "executing commnad1" | |
sleep 3 | |
} | |
command2() { | |
echo "executing commnad2" | |
sleep 3 | |
# エラーとして終了させる | |
exit 1 | |
} | |
echo "start" | |
# バックグラウンドで実行 | |
command1 & | |
# $!で直前に実行されたコマンドのプロセスIDを取得し、変数に保存している | |
pid1=$! | |
command2 & | |
pid2=$! | |
# waitは指定されたプロセスIDの処理が終わるまで待つ | |
wait $pid1 | |
# $?でwaitで指定しているプロセスの終了ステータスを取得することができる | |
if [ $? != 0 ]; then | |
echo "command1 error" | |
exit $? | |
else | |
echo "commnad1 success" | |
fi | |
wait $pid2 | |
if [ $? != 0 ]; then | |
echo "command2 error" | |
exit $? | |
else | |
echo "command2 success" | |
fi | |
echo "end" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment