Unable to run nohup command from jenkins as a background process

jenkins pipeline run background process
how to run jenkins in background
jenkins-pipeline run java command
cannot run program nohup jenkins
unix run command nohup
jenkins kill remote process
jenkins-pipeline run stage in background
solaris run command in background

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?

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!

Nohup doesn't work when executing script from Jenkins, 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  Also, if the workspace was not wiped out look for a file named nohup.out in whatever directory the shell command would have run. nohup automatically logs to such a file when the output is not otherwise redirected. – B Layer Jul 5 '17 at 13:15

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.

[JENKINS-32028] Statements after "nohup" skipped completely , But after running the nohup command, control does not go the next statement. I want to run the process in the background, and return to the command exit statements are not executed and job is completed without failure. When you run nohup command without ‘ &’ then it returns to shell command prompt immediately after running that particular command in the background. In the following example, nohup run bash command without ‘&’ to execute sleep1.sh file in the background.

In your jenkins shell script try:

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

It worked for me!

Running a background process in Pipeline job, Any one facing the same problem and using pipeline project, set JENKINS_NODE_COOKIE instead of BUILD_ID . Dont waste your time setting  I use the nohup command to restart tomcat server. Tomcat server was controlling both: 1) Application and 2) Jenkins. Jenkins is a CI tool. I have a script that stops and starts tomcat server from Jenkins. But after running stop script, tomcat server was shut down as a result Jenkins was not up. So Jenkins was unable to kick start script of tomcat.

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

Spawning processes from build - Jenkins, The background process (AKA daemon) inherits all the file descriptors of the environment where I can't install services or schedule background tasks. I launch a background command with the plugin Post build task and I've done this by just running the appium in shell as follow: BUILD_ID=dontKillMe nohup appium  Since there is no longer any association with the background task, you need to devise a background task that performs cmd2 then cmd1. This is cmd2; cmd1 (or rather cmd2 && cmd1 to run cmd1 only if cmd2 succeeds), and you'll need to tell nohup to start a shell running in the background for that. nohup sh -c './cmd2 >result2 && ./cmd1 >result1' &

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

Unix Nohup: Run a Command or Shell-Script Even after You Logout, When you execute a Unix job in the background ( using &, bg command), and logout In this quick tip, let us review how to make your process running even after you logout, using nohup. So Jenkins was unable to kick start script of tomcat. I use "nohup" command for starting the background process. However I could see the service started properly, but the log statements after nohup command are completely skipped. With the example provided in the description, echo "service running" and exit statements are not executed and job is completed without failure.

Using Jenkins pipeline, running a jar file in background using nohup , Using Jenkins pipeline, running a jar file in background using nohup does not launch the jar - spring-boot. The shell script works fine from command line. When I run the same in Jenkins' context, it fails. This is a Jenkins "feature" that kills any background processes started during a build when the build finishes, even if  In looking at nohup.c, nohup only ignores SIGHUP. You can also run a command like nohup sleep 30 from the command line. hit Ctrl+C, and observe that the sleep 30 process gets killed. The root problem is that nohup is a poor way to isolate a child process.

Starting a forever process in a Jenkins build step?, Starting a forever process in a Jenkins build step? deployment node.js jenkins. I'​m running a shell command at the end of a Jenkins deployment to restart a forever  Note that this will set the BUILD_ID environment variable for the process being spawned to something other than the current BUILD_ID. Or you can start jenkins with -Dhudson.util.ProcessTree.disable=true - see ProcessTreeKiller for details. On Windows, the 'at' command can be used to launch a process in the background. See the example below:

DevOps. Continuous integration with Jenkins as an example , Jenkins is an open source continuous integration project written in Java. and if the project launched with the wrong version, then the tests began to fail stably. the command will be executed in the background, and control will be returned running tasks do not end, you can use the nohup utility, that allows processes to  On Solaris 10 at least, you can use the nohup command afterwards nohup -p pid to prevent the running process to die when you leave your shell and to get its output in a file (nohup.out). I wasn't aware that any systems provided such an interface.

  • 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.
  • no it doesnt .. I have checked.
  • 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.
  • Also: what do you mean with "does not work". What output do you get from Bash?
  • Jenkins kills all spawned processes once the job ends. You can change BUILD_ID env variable to avoid this. See here and here.