if ステートメントを正しく動作させる方法、応答コードが常に 1 を表示し、ループから抜け出せない

if ステートメントを正しく動作させる方法、応答コードが常に 1 を表示し、ループから抜け出せない

アプリケーションが正常に再起動されたかどうかを確認しようとしています。アプリケーションの停止には問題はありませんが、アプリの起動には問題があります。アプリは起動しますが、スクリプトは実行し続け、サイトが復旧してもループから抜け出せません。

$aem_curl別のスクリプトを実行し、成功した場合は次の結果を表示し CheckHttp OK: 200, found /crxde/ in 11038 bytes、応答コードを返す変数があります0。ただし、失敗した場合は、CheckHttp CRITICAL: 503応答コードが表示されます。2

私のコード:

aem_curl="./check-http-aem.rb -h localhost -P 4502 -p /crx/de/index.jsp --response-code 200 -q 'crxde' -t 30"
STOP_TMOUT=15
echo "starting $AEM_APP this will take a few mins..." | ${LOG_FILE}
    sudo $restart_aem start
    count=0
    while true; do
      echo "Waiting for AEM to start try #${count} ..." | ${LOG_FILE}

        $aem_curl

     if [ $? -eq 0 ]; then
        echo "AEM has started! status code - $?" | ${LOG_FILE} && break
     else
        echo "AEM has not started yet - status code is $?" | ${LOG_FILE}
     fi
      if [ "$count" -eq "${STOP_TMOUT}" ]; then
              MESSAGE="Already waited 10 minutes for AEM start something is amiss." | ${LOG_FILE}
              exit 1
      fi
      count=$(($count+1))
      sleep 20
    done

私の出力:

Waiting for AEM to start try #0 ...
CheckHttp CRITICAL: Request error: Failed to open TCP connection to localhost:4502 (Connection refused - connect(2) for "localhost" port 4502)
AEM has not started yet - status code is 1
Waiting for AEM to start try #1 ...
CheckHttp CRITICAL: 503
AEM has not started yet - status code is 1
Waiting for AEM to start try #2 ...
CheckHttp CRITICAL: 503
...
Waiting for AEM to start try #19 ...
CheckHttp CRITICAL: 200, did not find /'crxde'/ in 11038 bytes: <!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=8"/><title>CRXDE Lite</title><link rel="shortcut icon" href="icons/crxde_favicon.ico"><link type="image/...
AEM has not started yet - status code is 1
Waiting for AEM to start try #20 ...
CheckHttp CRITICAL: 200, did not find /'crxde'/ in 11038 bytes: <!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=8"/><title>CRXDE Lite</title><link rel="shortcut icon" href="icons/crxde_favicon.ico"><link type="image/...
AEM has not started yet - status code is 1

答え1

次のようなことを試してみてください:

maxcount=15
count=0
while [ "$count" -lt "$maxcount" ] && ! ./check-http-aem.rb -h localhost -P 4502 -p /crx/de/index.jsp --response-code 200 -q 'crxde' -t 30; do
 ...
 sleep 20
 let count+=1
done

if [ "$count" -eq "$maxcount" ] ; then
  echo "AEM hasn't started" >/dev/stderr
  exit 1
fi

whileループは$count > $maxcountのいずれかになるまで実行されます。または ./check-http-aem.rb終了コード 0 (true) を返します。

$LOG_FILEところで、変数が何なのか、また、テキストをパイプで渡すときと "$MESSAGE" を設定するときがあるのはなぜか、私にはわかりません。また、変数の代わりにシェル関数を使用しないのはなぜか、あるいは のような既存のツールを使用しないのはなぜかlogger。質問とは関係がないので、stderr に何かを印刷するものとして無視します。


また...

シェルに頼ってスカラー変数を空白で分割するよりも、配列を使用してコマンド引数を保持する方が適切です。たとえば、次のようにします。

aem_args=(-h localhost -P 4502 -p /crx/de/index.jsp --response-code 200 -q 'crxde' -t 3)
./check-http-aem.rb "${aem_args[@]}"

または

aem_curl=./check-http-aem.rb
aem_args=(-h localhost -P 4502 -p /crx/de/index.jsp --response-code 200 -q 'crxde' -t 3)
$aem_curl "${aem_args[@]}"

の代わりに:

aem_curl="./check-http-aem.rb -h localhost -P 4502 -p /crx/de/index.jsp --response-code 200 -q 'crxde' -t 30"
$aem_curl

これらはすべて本質的に同じことを行いますが、後者では間違いを犯しやすく、予期しない動作が発生する可能性がはるかに高くなります。たとえば、後者の形式を使用して、スペース文字を含む引数をスクリプトに簡単に渡すことはできません。IFS 変数を変更しないと、他の、おそらく望ましくない結果が生じます。しかし、配列を使用すると、それを行うのは簡単です。

つまり、次のようにすることができます:

maxcount=15
count=0
aem_args=(-h localhost -P 4502 -p /crx/de/index.jsp --response-code 200 -q 'crxde' -t 3)

while [ "$count" -lt "$maxcount" ] && ! ./check-http-aem.rb "${aem_args[@]}"; do
 ...
 sleep 20
 let count+=1
done

関連情報