Restarting Tomcat after a successful deployment wi

2019-04-28 09:20发布

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.

2条回答
疯言疯语
2楼-- · 2019-04-28 09:39

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 &

查看更多
祖国的老花朵
3楼-- · 2019-04-28 09:44

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.

查看更多
登录 后发表回答