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]#

 

ProcessTreeKiller官方原文如下:

This feature is available since 1.260

To reliably kill processes spawned by a job during a build, Jenkins 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.ProcessTree.disable” to the value “true”.

This can be done as a parameter to the “java” binary when starting Jenkins:

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 Property hudson.util.ProcessTreeKiller.disable, but the class ProcessTreeKiller has been depecated since. For compatibility reasons, currently both property names are supported (as of version 1.404).

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 Jenkins‘s ProcessTreeKiller is looking for. This will cause Jenkins to assume that your daemon is not spawned by the Jenkins build. For example:

BUILD_ID=dontKillMe /usr/apache/bin/httpd

 

In case of Jenkins Pipeline use JENKINS_NODE_COOKIE instead of BUILD_ID

意思就大概是Jenkins每次构建完了后要自动杀掉部署过程中的子进程。

Jenkins使用processTreeKiller杀掉了所有子进程,而且这是Jenkins的默认行为。当一次build异常结束,或被人终止时,必然需要结束所有这次build启动的子进程。

解决jenkins自动杀掉衍生进程参照原文可以知道:

BUILD_ID=dontKillMe /usr/apache/bin/httpd

1.在执行 shell输入框中加入BUILD_ID=dontKillMe ,即可防止jenkins杀死启动的进程

ProcessTreeKiller Jenkins任务启动的后台进程被自动kill

  1. OLD_BUILD_ID=$BUILD_ID
  2. echo $OLD_BUILD_ID
  3. BUILD_ID=dontKillMe
  4. sh run.sh
  5. #改回原来的BUILD_ID值
  6. BUILD_ID=$OLD_BUILD_ID
  7. echo $BUILD_ID

2.临时改变BUILD_ID值,使得jenkins不会找到并结束掉run.sh启动的后台进程

ProcessTreeKiller Jenkins任务启动的后台进程被自动kill

 

ProcessTreeKiller Jenkins任务启动的后台进程被自动kill

上一篇:study6-shell流程控制


下一篇:ssh断连后,保持Linux后台程序连接