Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restarting Tomcat after a successful deployment with Jenkins

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.

like image 629
Joachim Avatar asked Oct 09 '14 12:10

Joachim


People also ask

How do you restart Tomcat?

Open the Services window (C:\Windows\system32\services. msc). Locate IDM Apps Tomcat Service. Select Start, Stop, or Restart.

Do I need to restart Tomcat?

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.

Can Jenkins and Tomcat run together?

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.

How often should I restart Tomcat?

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.


2 Answers

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 &

like image 172
Slav Avatar answered Sep 23 '22 12:09

Slav


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.

  1. First you need to install Post build task Plugin in Jenkins.
  2. Then create this script 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.

like image 29
Zeeshan Avatar answered Sep 19 '22 12:09

Zeeshan