Estou tentando verificar se um aplicativo foi reiniciado com êxito. Não tenho problemas em interrompê-lo, mas estou tendo problemas ao iniciá-lo. O aplicativo é iniciado, mas o script continua em execução e nunca sai do loop quando o site volta a funcionar.
Eu tenho um var $aem_curl
que executa outro script e exibe o seguinte resultado se for bem-sucedido
CheckHttp OK: 200, found /crxde/ in 11038 bytes
e fornece um código de resposta de 0
. Mas se falhar, ele exibe CheckHttp CRITICAL: 503
um código de resposta de2
Meu 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
Minha saída:
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
Responder1
Tente algo mais parecido com isto:
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
O loop while será executado até $count > $maxcountOU ./check-http-aem.rb
retorna um código de saída 0 (verdadeiro)
Aliás, não tenho ideia de qual $LOG_FILE
é a sua variável ou por que você está inserindo texto nela às vezes, mas definindo "$MESSAGE" em outra hora. Ou por que você não está usando uma função shell em vez de uma variável. Ou uma ferramenta existente como logger
. Não é realmente relevante para a questão, então vou fingir que imprime algo no stderr e ignorá-lo.
Também...
É melhor usar um array para armazenar argumentos de comando do que confiar no shell para dividir uma variável escalar em espaços em branco. Por exemplo, faça isso:
aem_args=(-h localhost -P 4502 -p /crx/de/index.jsp --response-code 200 -q 'crxde' -t 3)
./check-http-aem.rb "${aem_args[@]}"
ou
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[@]}"
em vez 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 fazem essencialmente a mesma coisa, mas é muito mais fácil cometer erros e obter comportamentos inesperados com estes últimos. Você não pode, por exemplo, usar a última forma para passar facilmente um argumento para o seu script que contenha um caractere de espaço, não sem alterar a variável IFS, o que terá outras consequências, provavelmente indesejadas. Mas usando um array, é trivial fazer isso.
Em outras palavras, você poderia fazer isso:
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