Running a background process in Pipeline job

27,095

Solution 1

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.

Solution 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'
                }
            }
        }
    }
}
Share:
27,095

Related videos on Youtube

Bradley Kreider
Author by

Bradley Kreider

Updated on September 18, 2022

Comments

  • Bradley Kreider
    Bradley Kreider over 1 year

    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.

    • Admin
      Admin almost 7 years
      What happens? Error message? Silent fail?
    • Admin
      Admin almost 7 years
      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.
    • Admin
      Admin almost 7 years
      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.
    • Admin
      Admin almost 7 years
      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.
    • Admin
      Admin almost 7 years
      @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.
    • Admin
      Admin almost 7 years
      Per this 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.
    • Admin
      Admin almost 7 years
      Tried that out. Same result process gets started and killed immediately.
  • nataraju
    nataraju almost 7 years
    Whoa, that's some inside baseball right there. Good to know.