if 문이 올바르게 작동하도록 하는 방법, 응답 코드는 항상 1을 표시하고 루프에서 벗어나지 않습니다.

if 문이 올바르게 작동하도록 하는 방법, 응답 코드는 항상 1을 표시하고 루프에서 벗어나지 않습니다.

애플리케이션이 성공적으로 다시 시작되었는지 확인하려고 합니다. 애플리케이션을 중지하는 데는 문제가 없지만 앱을 시작하는 데 문제가 있습니다. 앱이 시작되지만 스크립트는 계속 실행되며 사이트가 백업될 때 루프에서 벗어나지 않습니다.

나는 다른 스크립트를 실행하고 성공 $aem_curl하면 다음 결과를 표시하고 . 그러나 실패하면 다음 과 같은 응답 코드가 표시됩니다.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. 질문과 실제로 관련이 없으므로 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

관련 정보