有时我们希望将某个命令放在后台执行,然后并行(异步)执行其他命令。但在Jenkins上很容易遇到无法真正将命令放到后台运行的问题。
原因是pipeline在退出node{}的时候,会默认清理自己发出的每个shell命令,即使使用了nohup。
例如如下代码无法在后续的进程查找中找到top进程
pipeline {
agent {
label 'master'
}
stages {
stage('Example') {
steps {
script {
node("vm-1") {
sh "nohup top -b > /dev/null &"
}
node("vm-1") {
sh "ps -ef | grep top"
}
}
}
}
}
}
执行结果如下
[Pipeline] sh
+ grep top
+ ps -ef
tengrui 21907 21904 0 06:10 ? 00:00:00 grep top
解决方法是加入环境变量BUILD_ID=DONTKILLME
pipeline {
agent {
label 'master'
}
stages {
stage('Example') {
steps {
script {
node("vm-1") {
sh "BUILD_ID=dontKillMe nohup top -b > /dev/null &"
}
node("vm-1") {
sh "ps -ef | grep top"
}
}
}
}
}
}
运行结果如下
[Pipeline] sh
+ ps -ef
+ grep top
tengrui 21915 1 1 06:11 ? 00:00:00 top -b
tengrui 21925 21922 0 06:11 ? 00:00:00 grep top
注:
- 当pipeline退出node{}代码块的时候,才会清理shell进程
- 也可以使用JENKINS_NODE_COOKIE=dontKillMe