Keeping a build process running after Jenkins job
Asked Answered
R

4

6

I am trying to keep a web process running after the Jenkins job is done.

I looked into ProcessTreeKiller and have tried using BuildId as below, but it doesn't seem to work:

BUILD_ID=dontKillMe /usr/apache/bin/httpd

My command (which I want to keep it running):

rails s &

How can I fix this issue ?

enter image description here

Ripp answered 7/2, 2015 at 23:21 Comment(0)
N
6

You put that into your Execute Shell build step, not as a jenkins build process variable (which is what you did with EnvInject plugin in your screenshot)

So, if you are trying to run rails &, then do:
BUILD_ID=dontKillMe rails &

Norge answered 17/2, 2015 at 20:44 Comment(3)
i tried adding 'BUILD_ID=dontKillMe node app.js" before the line "node app.js" but i didn't work :(Franklyn
@johnlowvale not sure if typo or not, but did you include ` &` at the end?Norge
it still doesn't work, so i moved to use 'forever' and 'pm2' for my nodejs serverFranklyn
S
2

Try with:

(
  set -e
  export BUILD_ID=dontKillMe
  export JENKINS_NODE_COOKIE=dontKillMe
  rails &
) &
Sechrist answered 20/12, 2019 at 14:28 Comment(0)
S
0

If it is the last stage in your Jenkinsfile, (like my case), you can use BUILD_ID=dontKillMe to solve that problem, like this:

stage("Run"){
  steps{
    withEnv(['BUILD_ID=dontKillMe']) {
      script{
        sh '<YOUR COMMAND HERE>'
      }
    }
  }
}

In my case, I have Jenkins in Ubuntu, and I want to run the last stage of my jenkinsfile forever, so I used :

stage("Run app service forever"){
  steps{
    withEnv(['BUILD_ID=dontKillMe']) {
      script{
        kubeconfig(credentialsId: 'my_minikube_config_file', serverUrl: 'https://###.###.###.###:8443') {
          sh 'kubectl port-forward --address 0.0.0.0 services/tecapp 5000:5000'
        }
      }
    }
  }
}

Jenkins last stage running forever

NB: DON'T add & at the end of your command, like this :

stage("Run"){
  steps{
    withEnv(['BUILD_ID=dontKillMe']) {
      script{
        sh '<YOUR COMMAND HERE> &'
      }
    }
  }
}

when I used &, the stage didn't run forever.

Sandhog answered 28/3 at 12:21 Comment(0)
H
-2

start jenkin server by

java -Dhudson.util.ProcessTree.disable=true -jar jenkins.war --httpPort=8090

code to be in jenkinfile which goes in background

  stage("Starting API Server"){
    withEnv(['BUILD_ID=dontkill']) {
         sh 'mvn exec:java -Dexec.mainClass="APPLICATION_SERVER.App" & '
        }
   }
Hammer answered 12/8, 2017 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.