jenkins 的 ProcessTreeKiller

根据下面介绍,合适的解决方案是:

BUILD_ID=dontKillMe bash /home/app/opt/apache-tomcat-6.0.37/deploy-bin/deploy_test.sh

----------------

最近用jenkins执行服务器上的脚本,启动tomcat总是不成功,问了谷哥之后,发现是下面一个特性在作怪:

http://wiki.hudson-ci.org/display/HUDSON/ProcessTreeKiller

This feature is available since 1.260

To reliably kill processes spawned by a job during a build, Hudson contains a bit of native code to list up such processes and kill them. This is tested on several platforms and architectures, but if you find a show-stopper problem because of this, you can disable this feature by setting a Java property named "hudson.util.ProcessTreeKiller.disable" to the value "true".

This can be done as a parameter to the "java" binary when starting Hudson:

 java -Dhudson.util.ProcessTreeKiller.disable=true -jar hudson.war

Depending on how you run your container, this can be different. In the distributed build environment, this system property needs to be set on slave JVMs.

How it works

The ProcessTreeKiller takes advantage of the fact that by default a new process gets a copy of the environment variables of its spawning/creating process.

It sets a specific environment variable in the process executing the build job. Later, when the user requests to stop the build job's process it gets a list of all processes running on the computer and their environment variables, and looks for the environment variable that it initially set for the build job's process.

Every job with that environment variable in its environment is then terminated.

If your build wants to leave a daemon running behind...

A convenient way to achieve that is to change the environment variable BUILD_ID which Hudson's ProcessTreeKiller is looking for. This will cause Hudson to assume that your daemon is not spawned by the Hudson build. For example:

BUILD_ID=dontKillMe /usr/apache/bin/httpd

---------------------

来自stackoverflow的问答:

http://stackoverflow.com/questions/5728899/tomcat-script-not-working-when-run-from-hudson

http://stackoverflow.com/questions/7703156/start-up-tomcat-from-ant-script

----------------------

The problem had to do with Jenkins feature called ProcessTreeKiller described here.

Basically Jenkins automatically kills all processes spawned by a job by searching the process tree for processes with specific environment variable

All i had to do was to overwrite jenkins env variable called BUILD ID and it worked. I used a Setenv Plugin to set the env var specific for the build.

-----

What about executing the command like this :

<execexecutable="bash"><argvalue="-c"/><argvalue='nohup ${tomcat.bin.dir}/startup.sh -Xms128M -Xmx512M &'/></exec>
---

uild.properties file:

 #----------------------------------------------------
 #Tomcat Configuration
 #----------------------------------------------------
 #Back-end Tomcat 
 tomcat.dir=${branch.dir}/../tomcat
 tomcat.bin.dir=${tomcat.dir}/bin
 tomcat.bootstrap.jar=${tomcat.bin.dir}/bootstrap.jar
 tomcat.jvmarg=-Dcatalina.home

loadproperties file

<propertyfile="${basedir}/build.properties"/><!-- Stop tomcat --><targetname="stop-tomcat"description="Stops back-end tomcat server"depends="prepare"><javajar="${tomcat.bootstrap.jar}"fork="true"spawn="false"><jvmargvalue="${tomcat.jvmarg}=${tomcat.dir}"/><argline="${arg.stop}"/></java><echo>+---------------------------------+</echo><echo>|   T O M C A T   S T O P P E D   |</echo><echo>+---------------------------------+</echo></target>

Also I have added an element called spawn set to "false", which print execution output onto console. 

Hope this helps :) 
 

猜你喜欢

转载自alanland.iteye.com/blog/2047244