応答コードが常に1を表示し、ループから外れていない状態でifステートメントが正しく機能するようにするにはどうすればよいですか?

応答コードが常に1を表示し、ループから外れていない状態でifステートメントが正しく機能するようにするにはどうすればよいですか?

アプリケーションが正常に再起動されたことを確認しようとしています。アプリケーションの停止に問題はありませんが、アプリケーションの起動に問題があります。アプリケーションは起動しますが、スクリプトは実行され続け、サイトのバックアップ中はループは終了しません。

$aem_curl別のスクリプトを実行して成功すると、次の結果を表示し、応答コードを提供するvarがあります。ただし、失敗した場合は応答コードが表示されます。CheckHttp OK: 200, found /crxde/ in 11038 bytes0CheckHttp CRITICAL: 5032

私のコード:

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:。


返品...

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

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

おすすめ記事