ProcessTreeKiller Jenkins任務啟動的後臺程序被自動kill
https://www.whatled.com/post-6467.html
docker中啟動指令碼的路徑,直接加上這個引數即可,然後重啟Jenkins容器
[root@0249a3be5bc0 bin]# grep disable /usr/local/bin/jenkins.sh
exec java -Dhudson.util.ProcessTree.disable=true -Duser.home="$JENKINS_HOME" "${java_opts_array[@]}" -jar ${JENKINS_WAR} "${jenkins_opts_array[@]}" "$@"
[root@0249a3be5bc0 bin]#
This feature is available since 1.260
To reliably kill processes spawned by a job during a build,Jenkinscontains 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.ProcessTree.disable” to the value “true”.
This can be done as a parameter to the “java” binary when startingJenkins:
java -Dhudson.util.ProcessTree.disable=true -jar jenkins.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.
Older versions of Hudson (<1.315) used the Java Propertyhudson.util.ProcessTree
Killer.disable
, but the classProcessTreeKillerhas been depecated since. For compatibility reasons, currently both property names are supported (as of version 1.404).
How it works
TheProcessTreeKillertakes 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 whichJenkins‘sProcessTreeKilleris looking for. This will causeJenkinsto assume that your daemon is not spawned by theJenkinsbuild. For example:
BUILD_ID=dontKillMe /usr/apache/bin/httpd
|
In case of Jenkins Pipeline useJENKINS_NODE_COOKIE
instead ofBUILD_ID
意思就大概是Jenkins每次構建完了後要自動殺掉部署過程中的子程序。
Jenkins使用processTreeKiller殺掉了所有子程序,而且這是Jenkins的預設行為。當一次build異常結束,或被人終止時,必然需要結束所有這次build啟動的子程序。
解決jenkins自動殺掉衍生程序參照原文可以知道:
BUILD_ID=dontKillMe /usr/apache/bin/httpd
1.在執行 shell輸入框中加入BUILD_ID=dontKillMe
,即可防止jenkins殺死啟動的程序
- OLD_BUILD_ID=$BUILD_ID
- echo $OLD_BUILD_ID
- BUILD_ID=dontKillMe
- sh run.sh
- #改回原來的BUILD_ID值
- BUILD_ID=$OLD_BUILD_ID
- echo $BUILD_ID
2.臨時改變BUILD_ID值,使得jenkins不會找到並結束掉run.sh啟動的後臺程序