Jason
Jason

Reputation: 2492

Keeping a build process running after Jenkins job

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

Upvotes: 6

Views: 10357

Answers (4)

Aissam en
Aissam en

Reputation: 29

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.

Upvotes: 0

Eduardo Cuomo
Eduardo Cuomo

Reputation: 19016

Try with:

(
  set -e
  export BUILD_ID=dontKillMe
  export JENKINS_NODE_COOKIE=dontKillMe
  rails &
) &

Upvotes: 2

brahmananda Kar
brahmananda Kar

Reputation: 59

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" & '
        }
   }

Upvotes: -2

Slav
Slav

Reputation: 27515

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 &

Upvotes: 6

Related Questions