Monitor tomcat in Bash until it finishes deploying war or application

622 Views Asked by At

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

Scenarios:

  • Tomcat started with systemd
  • Tomcat started with catalina.sh
  • Using Tomcat Manager
  • Tomcat started from Eclipse
  • Embedded Tomcat on SpringBoot

My answer below.

1

There are 1 best solutions below

0
On BEST ANSWER

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"

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

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

With Tomcat Manager
With Tomcat manager configured:

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
If your Spring boot app uses Spring Boot Actuator and is configured as

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"