我试图检查一个应用程序是否已经重新启动成功,我没有问题停止应用程序,但有问题启动应用程序。该应用程序启动,但脚本继续运行,从来没有走出循环时,网站是备份。
我有一个var $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: CRXDE LiteCRXDE Lite发布于 2021-06-11 04:59:35
尝试更像这样的东西:
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
fiwhile循环将运行,直到$count > $maxcount OR ./check-http-aem.rb返回退出代码0 (true)为止。
顺便说一句,我不知道您的$LOG_FILE变量是什么,也不知道为什么有时您要将文本排入其中,但另一次设置"$MESSAGE“。或者为什么不使用shell函数而不是变量。或者像logger这样的现有工具。这与这个问题并没有什么关系,所以我就假装它打印了一些东西给stderr,然后忽略它。
还有..。
最好使用数组来保存命令参数,而不是依靠shell在空格上拆分标量变量。例如,执行以下操作:
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
donehttps://unix.stackexchange.com/questions/653759
复制相似问题