背景:
jenkins打包部署,对源码打成war包并部署到tomcat上,tomcat进行重启。
问题:
mv /root/.jenkins/workspace/Edaily-Core/edaily/edaily-web/target/*.war /root/.jenkins/workspace/Edaily-Core/edaily/edaily-web/target/edaily-web.war
cd /root/home/websoft/tomcat/apache-tomcat-7.0.94/webapps
rm -rf edaily-web
rm -rf edaily-web.war
mv /root/.jenkins/workspace/Edaily-Core/edaily/edaily-web/target/edaily-web.war /root/home/websoft/tomcat/apache-tomcat-7.0.94/webapps
kill -9 $(lsof -i:8087 | awk '{print $2}' | sed -n '2'p)
cd /root/home/websoft/tomcat/apache-tomcat-7.0.94/bin
unset CATALINA_HOME
./startup.sh
这是一个简易的脚本,没有很完善,主要就是“把打好的war移到tomcat的webapps目录下,关闭tomcat并重新启动tomcat”。
结果是,tomcat是关闭了但确没有重新启动。./startup.sh没起作用。
网上找了下原因,原因如下:
Jenkins会在构建完成后使用processTreeKiller杀掉了所有子进程,而且这是Jenkins的默认行为。这是processTreeKiller的介绍:https://wiki.jenkins-ci.org/display/JENKINS/ProcessTreeKiller。
我们来读一下:
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.
主要意思是:ProcessTreeKiller会保存着jenkins构建过程中的一些环境变量,当构建结束时,它会获取计算机上所有的进程和他们的环境变量,然后根据我们最初构建这个工程用到的环境变量A去匹配,哪些存在环境变量A的进程将会被结束。(和它有关的都杀死)
到这里我们理解了。
原来我们执行了./startup.sh之后,构建就结束了,系统马上杀死所有涉及到的进程,启动的进程也被杀死了,所以没有启动。
解决方案:
1、you can disable this feature by setting a Java property named "hudson.util.ProcessTree.disable" to the value "true".
也就是在jenkins的启动命令中增加:
java -Dhudson.util.ProcessTree.disable=true -jar jenkins.war
2、A convenient way to achieve that is to change the environment variable BUILD_ID which Jenkins's ProcessTreeKiller is looking for. This will cause Jenkins to assume that your daemon is not spawned by the Jenkins build. For example:
|
也就是jenkins是根据BUILD_ID这个环境变量去查找要杀死的进程,那么修改BUILD_ID可以让jenkins忽略这个进程(至于BUILD_ID 是啥可以随意,只要不和系统原来的BUILD_ID 相同即可),tomcat就可以启动了。