在 Bash 中监视 tomcat,直到它完成部署战争或应用程序 [英] Monitor tomcat in Bash until it finishes deploying war or application

查看:21
本文介绍了在 Bash 中监视 tomcat,直到它完成部署战争或应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 bash 脚本中监视 Tomcat 以检测它是否已完成部署战争或应用程序?

How could Tomcat be monitored in a bash script to detect that it finished deploying a war or application?

场景:

  • Tomcat 从 systemd
  • 开始
  • Tomcat 从 catalina.sh
  • 开始
  • 使用 Tomcat 管理器
  • Tomcat 从 Eclipse 启动
  • 在 SpringBoot 上嵌入 Tomcat

我的回答如下.

推荐答案

With systemd
如果您使用 systemctl 或 catalina.sh 使用 systemctl 启动 Tomcat,则可以完成类似的操作.将 demo.war 更改为您的战争名称,根据需要调整睡眠时间以显示尽可能多的 .(点).如果部署需要 30 秒,则会显示 30 个点.

With systemd
If you start Tomcat with systemctl or catalina.sh uses systemctl something like this could be done. Change demo.war to your war name, Adjust the sleep period to show as many . (dots) as needed. If it takes 30 secs to deploy, it will show 30 dots.

while ! systemctl status tomcat --no-pager | grep -q 'Deployment of.*demo.war.* has finished'; do \
echo -ne "."; sleep 1; \
done && echo "War deployed"

使用catalina.sh
如果 catalina.sh runApache 包 被使用(使用 catalina.sh start 将不起作用)

With catalina.sh
If catalina.sh run from Apache package is used (using catalina.sh start will not work)

while read -r line; do
    if echo "$line" | grep -q 'Deployment of.*[/]demo.war.* has finished' ; then
        echo "***** $line"
        # do some stuff with your app
        break
    else
        echo "---- $line"
    fi
done < <(./bin/catalina.sh run 2>&1 &) && echo "War deployed"
# start tomcat ----------^
# with console output but in background

./bin/catalina.sh stop

监控日志文件
可能需要对日志文件的权限.

Monitoring log file
Might required permissions to the log file.

log='/var/log/tomcat/catalina.2021-07-05.log'
msg='Deployment of.*[/]demo.war.* has finished'
while read -r line; do
    if echo "$line" | grep -q "$msg" ; then
        echo "***** $line"
        # do some stuff with your app
        break
    else
        echo "---- $line"
    fi
done < <(tail -n 1 -f "$log")
echo "War deployed"
sleep 5 # get a chance to see the previous message :-p

如果从 Eclipse 启动 tomcat

If starting tomcat from Eclipse

log='/home/lmc/workspace/.metadata/.plugins/org.eclipse.wst.server.core/logs/catalina.out'
msg='Deployment of deployment descriptor .*demo.xml. has finished'
# same code as above

使用 Tomcat 管理器
配置 Tomcat 管理器:

until ! curl -u userblah:s3cr3t --silent "http://localhost:8080/manager/text/list" | grep '^[/]demo:running'; do
    echo "Deploying 'demo' app"
    sleep 1
done

使用 SpringBoot 执行器
如果您的 Spring Boot 应用程序使用 Spring Boot Actuator 并配置为

management.endpoints.web.exposure.include=health
management.endpoints.web.base-path=/actuator
management.server.port=8081
management.server.address=127.0.0.1

可以监控为:

until curl --silent "http://127.0.0.1:8081/actuator/health" | grep -q 'status":"UP'; do
    echo "Deploying 'demo' app"
    sleep 1
done
echo "is UP"

这篇关于在 Bash 中监视 tomcat,直到它完成部署战争或应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆