Utsav Gupta
Utsav Gupta

Reputation: 3971

Unable to run nohup command from jenkins as a background process

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?

Upvotes: 11

Views: 25594

Answers (9)

Gokul Krishna
Gokul Krishna

Reputation: 1

vi /etc/sudoers

add line "jenkins ALL=(ALL) NOPASSWD: ALL" below wheels line now save it

go to bash execute the command "usermod -aG wheel jenkins"

use the nohup command like this "sudo nohup npx serve -s build -p 3007 > server.log 2>&1 &"

give permission for "server.log" once it created in your application path

it will work

Upvotes: -1

Dixit Kumar
Dixit Kumar

Reputation: 1

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)

Upvotes: 0

Tom CLERC
Tom CLERC

Reputation: 29

Simplest way :

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

Upvotes: -1

millevlada
millevlada

Reputation: 31

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 ';)

Upvotes: 0

Nik V
Nik V

Reputation: 93

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!

Upvotes: 3

Joao Vitor Marques
Joao Vitor Marques

Reputation: 314

In your jenkins shell script try:

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

It worked for me!

Upvotes: 8

Walid
Walid

Reputation: 142

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

Upvotes: 0

jpyams
jpyams

Reputation: 4364

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.

Upvotes: 9

Michu93
Michu93

Reputation: 5687

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

Upvotes: -1

Related Questions