Unable to run nohup command from jenkins as a background process

18,168

Solution 1

In your jenkins shell script try:

  export BUILD_ID=dontKillMe
  nohup java -jar your_java_app.jar &

It worked for me!

Solution 2

Long story short, Jenkins kills all processes spawned by a job once that job finishes. To override this behavior, you need to set an environment variable.

The variable appears to vary from job type to job type. It used to be BUILD_ID, but for Pipeline jobs it is JENKINS_NODE_COOKIE, and there are several others mentioned in this answer.

So if you're running your command in Pipeline, it would look like this:

sh 'JENKINS_NODE_COOKIE=dontKillMe nohup java -jar /home/.../jar/server-process-0.35.jar prod >> /var/../server-process-prod.log 2>&1 &'

See the wiki on ProcessTreeKiller and this comment in the Jenkins Jira for more information.

Solution 3

I tried every possible combination with BUILD_ID but it didn't work. I made it though by putting "nohup command > output.txt&" inside a shell script ran by the execute shell in jenkins, it worked perfectly!

Share:
18,168
Utsav Gupta
Author by

Utsav Gupta

Programmer at PeepApp Chai and wifi makes me high..

Updated on June 14, 2022

Comments

  • Utsav Gupta
    Utsav Gupta almost 2 years

    UPDATE: Based on below discussion I have edited my answer for more accurate description.

    I am trying to run a nohup command from jenkins. The full command is

    nohup java -jar /home/.../jar/server-process-0.35.jar prod >> /var/../server-process-prod.log 2>&1 &
    

    This command does not work. I can see status as success in jenkins but no java process in linux. When I do 'ps -ef | grep java'

    However when I remove the last '&' , that is I change it from run in forground instead of background

    It starts working. I can see the java process started.

    The original command works fine If I run it on linux console.

    I need to run it from jenkins in the original form that is as a backgorund process. So that it is independant of jenkins.

    Any clues why is this happening?

  • Gank
    Gank almost 4 years
    + sh 'JENKINS_NODE_COOKIE=dontKillMe nohup java -Xms10m -Xmx200m -jar /var/lib/jenkins/bladexTarget/target/blade-auth.jar &' sh: JENKINS_NODE_COOKIE=dontKillMe nohup java -Xms10m -Xmx200m -jar /var/lib/jenkins/bladexTarget/target/blade-auth.jar &: 没有那个文件或目录 Build step 'Execute shell' marked build as failure Finished: FAILURE