Answers: <p>Alternative solution. Use the <code>parallel</code> pipeline step to run your foreground and background processes as equal partners in, well, parallel.</p>
  <p>One advantage is that you have easy access to the stdout of the background process in the Blue Ocean UI.</p>
  <pre><code>stage(&quot;Run&quot;) {
      parallel {
          stage('k3s') {
              steps {
                  sh 'sudo /home/jenkins/k3s server --write-kubeconfig-mode 664 &amp; echo $! &gt; $WORKSPACE/k3s.pid; wait -n $(cat $WORKSPACE/k3s.pid) || true'
              }
          }
          stage('test') {
              steps {
                  sh 'sleep 1; ps -p $(cat $WORKSPACE/k3s.pid)'
                  sh 'while ! /home/jenkins/k3s kubectl get node; do sleep 1; done'
              }
              post {
                  always {
                      sh 'while ps -p $(cat $WORKSPACE/k3s.pid); do sudo kill -9 $(cat $WORKSPACE/k3s.pid); sleep 5; done'
                  }
              }
          }
      }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-08-10T18:17:19.290Z
Answers: 2020-08-10T18:57:02.670Z
Answers: 2020-08-10T18:57:02.670Z
Answers: user7610
Answers: <p>With a <a href="https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#bat-windows-batch-script" rel="nofollow noreferrer"><code>bat</code> pipeline step</a> it works with:</p>
  <pre class="lang-groovy prettyprint-override"><code>            script {
                  withEnv ( ['JENKINS_NODE_COOKIE=do_not_kill'] ) {
  
                     bat &quot;&quot;&quot;
                         @set prompt=\$G\$S
                         cd ${RUN_DIR}
                         start \&quot;title\&quot; my.bat
                         start \&quot;title\&quot; my.cmd
                         start \&quot;title\&quot; my.exe
                         &quot;&quot;&quot;
                  }
              }
   
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2023-01-31T13:05:56.360Z
Answers: Thanks . I've been looking for this jem for couple of hours. This solution actually works
Answers: Mike
Answers: CC BY-SA 4.0
Answers: 2023-08-27T10:42:06.470Z
Answers: why are there underscores, shouldnt it be `doNotKill`?
Answers: Nor.Z
Answers: CC BY-SA 4.0
Answers: 2021-09-12T21:59:47.593Z
Answers: 2021-09-12T21:59:47.593Z
Answers: Gerold Broser
Answers: <p>Any one facing the same problem and using pipeline project, set <code>JENKINS_NODE_COOKIE</code> instead of <code>BUILD_ID</code>.</p>
  
  <p>Dont waste your time setting <code>HUDSON_COOKIE</code>, <code>HUDSON_SERVER_COOKIE</code>, <code>JENKINS_COOKIE</code> or <code>JENKINS_SERVER_COOKIE</code>. None of them work for pipeline project.</p>
  
  <p>Refer to <a href="https://issues.jenkins-ci.org/browse/JENKINS-28182" rel="noreferrer">https://issues.jenkins-ci.org/browse/JENKINS-28182</a> for more details.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-07-16T07:18:52.890Z
Answers: Whoa, that's some inside baseball right there. Good to know.
Answers: B Layer
Answers: CC BY-SA 3.0
Answers: 2017-07-11T12:25:29.397Z
Answers: 2017-11-02T06:31:01.213Z
Answers: 2017-11-02T06:31:01.213Z
Answers: Dharanidhar
Body: <p>I am looking for a way to run a java process in background using Jenkins declarative pipeline. Below is the snippet of code</p>
  
  <pre><code>stage('Deploy'){
          steps{
              script{
                  withEnv(['BUILD_ID=dontkill']) {
                      sh "nohup java -jar test-0.0.1-SNAPSHOT.war &amp;"
                  }
              }
          }
  }
  </code></pre>
  
  <p>Already aware of ProcessTreeKiller of Jenkins and every answer suggest to do the same. I have tried using environment inside the steps block with no luck.</p>
  
  <p>Jenkins version : 2.60.1</p>
  
  <p>Pipeline plugin : 1.1.7</p>
  
  <p>Any help is greatly appreciated.</p>
  
Comments: CC BY-SA 3.0
Comments: 2017-07-05T08:55:19.553Z
Comments: What happens? Error message? Silent fail?
Comments: B Layer
Comments: CC BY-SA 3.0
Comments: 2017-07-05T08:57:53.170Z
Comments: I do not have a running java process. This is the final stage in my pipeline so my guess is that jenkins starts it and terminates it the very next moment.
Comments: Dharanidhar
Comments: CC BY-SA 3.0
Comments: 2017-07-05T13:13:05.097Z
Comments: What happens when you run the command directly on the slave? Log into the agent/slave host, go to the job's workspace (or if it was cleaned up manually copy in the war file somewhere...`/tmp` for example), and run everything between the double-quotes.
Comments: B Layer
Comments: CC BY-SA 3.0
Comments: 2017-07-05T13:15:57.497Z
Comments: Also, if the workspace was not wiped out look for a file named nohup.out in whatever directory the shell command would have run. nohup automatically logs to such a file when the output is not otherwise redirected.
Comments: B Layer
Comments: CC BY-SA 3.0
Comments: 2017-07-06T07:57:01.893Z
Comments: @BlairM the same script runs fine when i try it with jenkins user via a non-login non-interactive shell. Running in double quotes makes no difference. nohup.out is present but it is 0 KB.
Comments: Dharanidhar
Comments: CC BY-SA 3.0
Comments: 2017-07-06T21:39:28.947Z
Comments: Per this https://stackoverflow.com/a/40514899 suggest you invoke java from within a script instead of calling directly. And instead of using `withEnv` put BUILD_ID=dontKillme on the command line. There's a subtle difference between the two methods.
Comments: B Layer
Comments: CC BY-SA 3.0
Comments: 2017-07-10T12:04:41.510Z
Comments: Tried that out. Same result process gets started and killed immediately.
Comments: Dharanidhar
Tags: <jenkins><jenkins-pipeline><jenkinsfile>
Title: Running a background process in Pipeline job
UserDisplayName: Dharanidhar
ThreadText: # Running a background process in Pipeline job
  
  (Asked by: username_0 on 2017-07-03)
  
  I am looking for a way to run a java process in background using Jenkins declarative pipeline. Below is the snippet of code
  
  ```
  stage('Deploy'){
          steps{
              script{
                  withEnv(['BUILD_ID=dontkill']) {
                      sh "nohup java -jar test-0.0.1-SNAPSHOT.war &"
                  }
              }
          }
  }
  
  ```
  
  Already aware of ProcessTreeKiller of Jenkins and every answer suggest to do the same. I have tried using environment inside the steps block with no luck.
  
  Jenkins version : 2.60.1
  
  Pipeline plugin : 1.1.7
  
  Any help is greatly appreciated.
  
  **Tags:** jenkins, jenkins-pipeline, jenkinsfile
  
  > **Comments:**
  > **username_1**: What happens? Error message? Silent fail?  
  > **username_0**: I do not have a running java process. This is the final stage in my pipeline so my guess is that jenkins starts it and terminates it the very next moment.  
  > **username_1**: What happens when you run the command directly on the slave? Log into the agent/slave host, go to the job's workspace (or if it was cleaned up manually copy in the war file somewhere...`/tmp` for example), and run everything between the double-quotes.  
  > **username_1**: Also, if the workspace was not wiped out look for a file named nohup.out in whatever directory the shell command would have run. nohup automatically logs to such a file when the output is not otherwise redirected.  
  > **username_0**: @BlairM the same script runs fine when i try it with jenkins user via a non-login non-interactive shell. Running in double quotes makes no difference. nohup.out is present but it is 0 KB.  
  > **username_1**: Per this https://stackoverflow.com/a/40514899 suggest you invoke java from within a script instead of calling directly. And instead of using `withEnv` put BUILD_ID=dontKillme on the command line. There's a subtle difference between the two methods.  
  > **username_0**: Tried that out. Same result process gets started and killed immediately.  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_0 on 2017-07-11. Score: 22 | Accepted answer
  
  Any one facing the same problem and using pipeline project, set `JENKINS_NODE_COOKIE` instead of `BUILD_ID`.
  
  Dont waste your time setting `HUDSON_COOKIE`, `HUDSON_SERVER_COOKIE`, `JENKINS_COOKIE` or `JENKINS_SERVER_COOKIE`. None of them work for pipeline project.
  
  Refer to <https://issues.jenkins-ci.org/browse/JENKINS-28182> for more details.
  
  > **Comments:**
  > **username_1**: Whoa, that's some inside baseball right there. Good to know.  
  
  ---
  
  ### Answer by username_2 on 2020-08-10. Score: 2
  
  Alternative solution. Use the `parallel` pipeline step to run your foreground and background processes as equal partners in, well, parallel.
  
  One advantage is that you have easy access to the stdout of the background process in the Blue Ocean UI.
  
  ```
  stage("Run") {
      parallel {
          stage('k3s') {
              steps {
                  sh 'sudo /home/jenkins/k3s server --write-kubeconfig-mode 664 & echo $! > $WORKSPACE/k3s.pid; wait -n $(cat $WORKSPACE/k3s.pid) || true'
              }
          }
          stage('test') {
              steps {
                  sh 'sleep 1; ps -p $(cat $WORKSPACE/k3s.pid)'
                  sh 'while ! /home/jenkins/k3s kubectl get node; do sleep 1; done'
              }
              post {
                  always {
                      sh 'while ps -p $(cat $WORKSPACE/k3s.pid); do sudo kill -9 $(cat $WORKSPACE/k3s.pid); sleep 5; done'
                  }
              }
          }
      }
  }
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2021-09-12. Score: 1
  
  With a [`bat` pipeline step](https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#bat-windows-batch-script) it works with:
  
  ```groovy
              script {
                  withEnv ( ['JENKINS_NODE_COOKIE=do_not_kill'] ) {
  
                     bat """
                         @set prompt=\$G\$S
                         cd ${RUN_DIR}
                         start \"title\" my.bat
                         start \"title\" my.cmd
                         start \"title\" my.exe
                         """
                  }
              }
   
  
  ```
  
  > **Comments:**
  > **username_4**: Thanks . I've been looking for this jem for couple of hours. This solution actually works  
  > **username_5**: why are there underscores, shouldnt it be `doNotKill`?  
  
  ---
  
  