Skip to content

Instantly share code, notes, and snippets.

@vsachinv
Last active April 26, 2020 17:05
Show Gist options
  • Select an option

  • Save vsachinv/93884225715fe6a8927d8062c2face3e to your computer and use it in GitHub Desktop.

Select an option

Save vsachinv/93884225715fe6a8927d8062c2face3e to your computer and use it in GitHub Desktop.
Bash script to deploy war file over tomcat after build is copied on the server. Alternate to tomcat manager if you don't have that.
#!/bin/bash
# Author Sachin Verma
##Validate below variable based on env.
TOMCAT_USER=root
TOMCAT_HOME="/u01/apache-tomcat-8.5.24"
SHUTDOWN_WAIT=45
TOMCAT_WEB_APPS_DIR=$TOMCAT_HOME'/webapps'
WEB_APP=$TOMCAT_WEB_APPS_DIR'/reports'
JENKINS_BUILD_WAR_FILE='/tmp/reports.war'
WEB_APP_HEALTH_URL='https://localhost:8443/reports/ping/alihoih398yewkj3298hfeiub'
APP_UP_WAIT=900
TOMCAT_WRK_DIR=$TOMCAT_HOME'/work/'
TOMCAT_STOP_SCRIPT=$TOMCAT_HOME'/bin/shutdown.sh'
TOMCAT_START_SCRIPT=$TOMCAT_HOME'/bin/startup.sh'
tomcat_pid() {
#TODO need to fix when multiple tomcats running in same env.
echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
}
startTomcat() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is already running (pid: $pid)"
else
# Start tomcat
echo "Starting tomcat"
/bin/su - -c "cd $TOMCAT_HOME && $TOMCAT_START_SCRIPT" $TOMCAT_USER
fi
return 0
}
stopTomcat() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Stopping Tomcat"
/bin/su - -c "cd $TOMCAT_HOME && $TOMCAT_STOP_SCRIPT" $TOMCAT_USER
let kwait=$SHUTDOWN_WAIT
count=0
count_by=5
until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
do
echo "Waiting for processes to exit. Timeout before we kill the pid: ${count}/${kwait}"
sleep $count_by
let count=$count+$count_by;
done
if [ $count -gt $kwait ]; then
echo "Killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
kill -9 $pid
fi
else
echo "Tomcat is not running"
fi
return 0
}
copyNewWebAppBuild() {
echo "Coping APP war file " $JENKINS_BUILD_WAR_FILE " to the tomcat web apps folder " $TOMCAT_WEB_APPS_DIR
/bin/su - -c "cd $TOMCAT_WEB_APPS_DIR && cp $JENKINS_BUILD_WAR_FILE $TOMCAT_WEB_APPS_DIR" $TOMCAT_USER
echo "Copied APP war file " $JENKINS_BUILD_WAR_FILE " to the tomcat web apps folder " $TOMCAT_WEB_APPS_DIR
return 0
}
deleteOldWebAppBuild(){
echo "Deleting the Old WebApp build content"
/bin/su - -c "cd $TOMCAT_WEB_APPS_DIR && rm -r $WEB_APP" $TOMCAT_USER
echo "Deleted the Old WebApp build content"
# Optional step
echo "Deleting the tomcat work folder " $TOMCAT_WRK_DIR
/bin/su - -c "cd $TOMCAT_WRK_DIR && rm -r $TOMCAT_WRK_DIR/*" $TOMCAT_USER
echo "Deleted the tomcat work folder " $TOMCAT_WRK_DIR
return 0
}
checkAppStatus(){
let kwait=$APP_UP_WAIT
count=0
count_by=60
until [ `curl --silent --insecure -I $WEB_APP_HEALTH_URL | grep -c 'HTTP/1.1 200'` = '1' ] || [ $count -gt $kwait ]
do
echo "Waiting for Application to be up: ${count}/${kwait}"
sleep $count_by
let count=$count+$count_by;
done
if [ $count -gt $kwait ]; then
echo "Application is not up yet deployment failed."
else
echo "Application is up as well."
fi
return 0
}
stopTomcat
sleep 5;
deleteOldWebAppBuild
sleep 5;
copyNewWebAppBuild
sleep 5;
startTomcat
sleep $SHUTDOWN_WAIT;
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is running with pid: $pid"
checkAppStatus
else
echo "Tomcat is not running and error occurred."
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment