How can I have Jenkins restart a Tomcat instance after a successful deployment?
I already tried using a batch script, but the Tomcat instance is killed when the build is finished.
Open the Services window (C:\Windows\system32\services. msc). Locate IDM Apps Tomcat Service. Select Start, Stop, or Restart.
You shouldn't need to restart Tomcat, but you will need to redeploy your application, or use a way of deployment that supports hot reloading. You can add reloadable="true" in your context file (e.g. the default one conf/context. xml ). Cf.
Apache Tomcat is a powerful servlet Java container for running web applications. If you are running your apps in Tomcat, or wish to do so, you might also want to run Jenkins in it. This creates a unified experience, which is easier to manage.
We recommend restarting the Tomcat service and clearing tomcat logs and temp files periodically. This can vary from once a week to once a day, depending on how the system is using the system Memory, the number of users connected simultaneously, or the amount of objects set in cache memory.
Your answer lies in Jenkins ProcessTreeKiller. A more detailed explanation here.
It's a design decision to kill any processes that are spawned by the build to maintain a clean environment. Unfortunately that means you can't leave a process (such as Tomcat) running after the build.
You can disable this functionality globally (not recommended) by launching Jenkins like this:java -Dhudson.util.ProcessTree.disable=true -jar jenkins.war
Or you can disable this on a per-case basis, by launching the process with a changed environment variable:BUILD_ID=dontKillMe ./catalina restart
Some people report however that changing BUILD_ID
is not enough. They recommend also unsetting:JENKINS_COOKIE
JENKINS_SERVER_COOKIE
Edit:
Another issue that could be at play is that when you connect to remote shell, and start a process in that remote shell session, once you (Jenkins) disconnects, the session is killed and all processes spawned by the session are killed too. To get around that issue, you need to disassociate the process from the shell session.
One way is:
nohup ./catalina restart &
This is how I am restarting Tomcat after deployment via jenkins.
I am having two servers DEV and QA where I need to do the deployment and restart tomcat. I have Jenkins installed in DEV server.
tomcat-restart.ksh
in the server where you have tomcat installed..#!/bin/bash
echo "*********************Restarting Tomcat70.******************"
sh /apps/apache/sss-tomcat70.ksh status
echo "Trying to stop Tomcat."
sh /apps/apache/sss-tomcat70.ksh stop
echo "Getting Tomcat Status."
sh /apps/apache/sss-tomcat70.ksh status
echo "Trying to Start Tomcat"
sh /apps/apache/sss-tomcat70.ksh start
sleep 2
echo "Getting Tomcat Status"
sh /apps/apache/sss-tomcat70.ksh status
Restarting Tomcat on DEV server.
Since Jenkins and Tomcat is installed in the same machine I am directly calling the script.
In Jenkins go to Add post-build action
and choose Post build task
and in the Script
textbox add the following : /apps/apache/tomcat-restart.ksh
Restarting Tomcat in QA server.
Since Jenkins is installed in different server, I am calling the script to restart Tomcat via Secure Shell.
In Jenkins go to Add post-build action
select Post build task
and in the Script textbox add the following : sshpass -p 'myPassword' ssh -tt username@hostname sudo sh /apps/apache/tomcat-restart.ksh
You need to install sshpass
if its not already installed.
If everything went fine, then you may see something like this in your Jenkins log.
Running script : /apps/apache/tomcat-restart.ksh
[workspace] $ /bin/sh -xe /tmp/hudson43653169595828207.sh
+ /apps/apache/tomcat-restart.ksh
*********************Restarting Tomcat70.*********************
Tomcat v7.0 is running as process ID 3552
*********************Trying to stop Tomcat.*********************
Stopping Tomcat v7.0 running as process ID 3552...
*********************Getting Tomcat Status.*********************
Tomcat v7.0 is not running
*********************Trying to Start Tomcat*********************
Starting Tomcat v7.0 server...
*********************Getting Tomcat Status*********************
Tomcat v7.0 is running as process ID 17969
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With