Restarting Tomcat after a successful deployment with Jenkins

馋奶兔 提交于 2021-02-18 22:26:11

问题


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.


回答1:


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 &




回答2:


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.



来源:https://stackoverflow.com/questions/26278117/restarting-tomcat-after-a-successful-deployment-with-jenkins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!