11

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?

Utsav Gupta
  • 3,785
  • 5
  • 30
  • 52
  • it is not that is not working, it is that it goes to the background because you write `command ... &`. You can look for it checking the output of `jobs` or with `ps -ef | grep nohup`. – fedorqui May 20 '16 at 08:32
  • 1
    no it doesnt .. I have checked. – Utsav Gupta May 20 '16 at 08:35
  • 1
    Does it work if you say the same without `&`? That is, `nohup java -jar /home/.../jar/server-process-0.35.jar prod >> /var/../server-process-prod.log 2>&1`. – fedorqui May 20 '16 at 08:36
  • Also: what do you mean with "does not work". What output do you get from Bash? – fedorqui May 20 '16 at 08:38
  • I see it starts working after removing & as you said. By not working I mean the java process does not start , i.e on doing "ps -ef | grep java " I dont see the process listed. However If I remove & I see the java process listed when i run from jenkins. When I type the command on linux console with '&' i see the java process running there too. So this does not solve my problem totally as I need the '&' to make it run in backgorund and also need to run it from jenkins. – Utsav Gupta May 20 '16 at 08:42
  • It is strange: I tested against something as simple as `nohup sleep 10 >> output 2>&1 &` and it works fine with and without `&`. – fedorqui May 20 '16 at 08:44
  • Then this is something related to Jenkins, so you'll need to provide more details in how you are configuring it. Maybe you have to escape the `&` in order to be interpreted properly by Jenkins. – fedorqui May 20 '16 at 09:23
  • I am thinking on exact same lines, could it be an escaping '&' problem? – Utsav Gupta May 20 '16 at 09:27
  • To answer your question I have added a build step 'execute shell' in which I have saved the above command as is – Utsav Gupta May 20 '16 at 09:32
  • However, the command also has the & in `2>&1`, so this should not matter. I don't know, there must be something Jenkins related in this. – fedorqui May 20 '16 at 09:47
  • 3
    Jenkins kills all spawned processes once the job ends. You can change BUILD_ID env variable to avoid this. See [here](http://stackoverflow.com/questions/9567441/jenkins-seems-to-be-the-target-for-nohup-in-a-script-started-via-ssh-how-can-i) and [here](https://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build). – akhan Sep 11 '16 at 17:50

8 Answers8

9

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.

jpyams
  • 4,030
  • 9
  • 41
  • 66
  • + 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``` – Gank Jun 01 '20 at 08:02
8

In your jenkins shell script try:

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

It worked for me!

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!

Nik V
  • 91
  • 2
  • 10
0

Got the same problem, added:

BUILD_ID=dontKillMe python /var/lib/jenkins/release.py

into Execute Shell -> Command and inside release.py there is:

os.system('nohup java -jar ' + new_jars_on_server + '/' + generated_jar_by_mvn_name + '&')

and it works

Michu93
  • 5,058
  • 7
  • 47
  • 80
0

Best simple solution is to use "at now" instead of "nohup"

In your job jenkins (execute shell) put :

set +e #so "at now" will run even if java -jar fails
#Run java app in background
echo "java -jar $(ls | grep *.jar | head -n 1)" | at now + 1 min
Walid
  • 142
  • 1
  • 10
0

what worked for me was wrapping the nohup java -jar ... command into sh file inside execute shell command, and running that same sh file right after:

echo "starting java jar..."
cd [some location where jar is]
echo "nohup java -jar [jar_name].jar &" > start-jar-in-background.sh
sh start-jar-in-background.sh
echo "started java jar"

If I had nohup java -jar ... inline with Execute shell command, then it didn't start it from some reasons. I spent quite some time on this, hope it helps to someone ';)

millevlada
  • 31
  • 6
0

Simplest way :

`nohup java -jar [jar_name].jar >log_file_you_want 2>another_file`&

Tom CLERC
  • 11
  • 1
  • 5
0
set +e #so "at now" will run even if java -jar fails

#Run java app in background
echo "java -jar $(ls | grep *.jar | head -n 1)" | at now + 1 min

above command worked for him, thanks @walid, & remove at the end (+ 1 min)

BB9z
  • 2,432
  • 1
  • 30
  • 36