What's the proper way to launch a script from jenkins, don't get the build hanging, and leave the process running? I can't seem to get it to work. Either the script doesn't run or the build hangs.
If I put in the build's "Execute shell" step bash relaunch.sh &
or relaunch.sh > output.log &
or nohup bash relaunch.sh &
, nothing happens; build finishes, but the process doesn't run. I guess it can be related to Jenkins waiting for the error pipe to close.
If I do nohup bash relaunch.sh 2>&1 > output.log
as suggested here, the output is properly redirected, but the build hangs (doesn't finish), and the process dies when I kill the build.
Adding export BUILD_ID=dontKillMe
, as suggested here, here, and here, either to the "Execute shell" step or the script itself doesn't help either. The build hangs and the process dies when I kill the build. Needless to say, my knowledge of linux is very limited.
How do people do this in a clean way?
Running shell command or script in background using nohup command. Another way you can run a command in the background is using the nohup command. The nohup command, short for no hang up, is a command that keeps a process running even after exiting the shell.
If you want to push a command into the background, using & at the end is an easy way to do that. This way, you can issue a command in the background and continue to use your terminal as it runs. It comes with a catch, though. Using & doesn't disconnect the command away from you; it just pushes it into the background.
A convenient way to achieve that is to change the environment variable BUILD_ID
under Execute shell which Jenkins's
ProcessTreeKiller is looking for.
By doing,
BUILD_ID=dontKillMe nohup bash relaunch.sh &
Jenkins will assume that the background job is not spawned by the build and will not kill them after finishing the job.
Thanks to Joshua for his observation that you could also use JENKINS_NODE_COOKIE
as
JENKINS_NODE_COOKIE=dontKillMe
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With