简体   繁体   English

在 Bash 中监视 tomcat,直到它完成部署战争或应用程序

[英]Monitor tomcat in Bash until it finishes deploying war or application

How could Tomcat be monitored in a bash script to detect that it finished deploying a war or application?如何在 bash 脚本中监视Tomcat以检测它是否已完成部署战争或应用程序?

Scenarios:场景:

  • Tomcat started with systemd Tomcat 以systemd启动
  • Tomcat started with catalina.sh Tomcat 以catalina.sh启动
  • Using Tomcat Manager使用 Tomcat 管理器
  • Tomcat started from Eclipse Tomcat 从 Eclipse 启动
  • Embedded Tomcat on SpringBoot SpringBoot 上的嵌入式 Tomcat

My answer below.我的回答如下。

With systemd使用systemd
If you start Tomcat with systemctl or catalina.sh uses systemctl something like this could be done.如果您使用 systemctl 或 catalina.sh 使用 systemctl 启动 Tomcat,则可以完成类似的操作。 Change demo.war to your war name, Adjust the sleep period to show as many .demo.war更改为您的战争名称,调整睡眠时间以显示尽可能多的. (dots) as needed. (点)根据需要。 If it takes 30 secs to deploy, it will show 30 dots.如果部署需要 30 秒,它将显示 30 个点。

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

With catalina.sh使用catalina.sh
If catalina.sh run from Apache package is used (using catalina.sh start will not work)如果使用从Apache 包catalina.sh run catalina.sh start (使用catalina.sh start将不起作用)

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

If starting tomcat from Eclipse如果从 Eclipse 启动 tomcat

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

With Tomcat Manager使用 Tomcat 管理器
With Tomcat manager configured:配置Tomcat 管理器后

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

With SpringBoot Actuator使用 SpringBoot 执行器
If your Spring boot app uses Spring Boot Actuator and is configured as如果您的 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

It can be monitored as:它可以被监控为:

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM