cómo hacer que mi declaración if funcione correctamente, el código de respuesta siempre muestra 1 y nunca sale del bucle

cómo hacer que mi declaración if funcione correctamente, el código de respuesta siempre muestra 1 y nunca sale del bucle

Estoy tratando de verificar si una aplicación se ha reiniciado exitosamente. No tengo problemas para detener la aplicación, pero sí tengo problemas para iniciarla. La aplicación se inicia pero el script continúa ejecutándose y nunca sale del bucle cuando el sitio vuelve a funcionar.

Tengo una var $aem_curlque ejecuta otro script y muestra el siguiente resultado si tiene éxito CheckHttp OK: 200, found /crxde/ in 11038 bytesy proporciona un código de respuesta de 0. Pero si falla, muestra CheckHttp CRITICAL: 503un código de respuesta de2

Mi código:

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

Mi salida:

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

Respuesta1

Pruebe algo más como esto:

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

El bucle while se ejecutará hasta que $count > $maxcountO ./check-http-aem.rbdevuelve un código de salida de 0 (verdadero)

Por cierto, no tengo idea de cuál $LOG_FILEes su variable o por qué a veces inserta texto en ella pero configura "$ MESSAGE" en otra ocasión. O por qué no estás usando una función de shell en lugar de una variable. O una herramienta existente como logger. No es realmente relevante para la pregunta, así que fingiré que imprime algo en stderr y lo ignoraré.


También...

Es mejor usar una matriz para contener los argumentos del comando que confiar en el shell para dividir una variable escalar en espacios en blanco. Por ejemplo, haz esto:

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

o

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[@]}"

en lugar de:

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

Todos estos hacen esencialmente lo mismo, pero es mucho más fácil cometer errores y tener un comportamiento inesperado con este último. No puede, por ejemplo, utilizar esta última forma para pasar fácilmente un argumento a su script que contenga un carácter de espacio, no sin cambiar la variable IFS, lo que tendrá otras consecuencias probablemente no deseadas. Pero usar una matriz es trivial.

En otras palabras, podrías hacer esto:

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

información relacionada