Background job is killed after Jenkins job finishes

Hi, I am trying to start a background process by running a shell script from the jenkins jobs. The problem is as soon as the job completes the background job is getting killed. I have tried running it with nohup as well as by giving parameter “BUILD_ID=dontKillMe” but it didn’t work.

It will be a big help if someone could guide me on this.

Jenkins kills all processes started by job once the job is finished to avoid that you have runaway processes.
Starting background jobs from Jenkins is not the right approach I think because when the job runs multiple times it will try to start the daemon multiple times. So you would need to find a way to reliable kill the background process once the Jenkins job runs again.

Technically it is possible to stop Jenkins from killing the daemon process
https://wiki.jenkins.io/display/JENKINS/ProcessTreeKiller
Just don’t pass it as a job parameter and for pipeline jobs you have to use JENKINS_NODE_COOKIE instead of BUILD_ID

Hi @mawinter69, I tried this but it didn’t worked. Can you help with any other alternatives.

Are you doing this in a pipeline job or a freestyle job?
Can you share the script that you run?
e.g. in a pipeline script you should do something like this:

sh '''
  JENKINS_NODE_COOKIE=dontKillMe /mybackgroundexecutable &
'''

I am running as freestyle job.

command :-
daemonize -E BUILD_ID=dontKillMe nohup $SPLEBASE/bin/threadpoolworker.sh -d Y -p DEFAULT=5 >/dev/null 2>&1 &

If that is the last command in the script you can try

export BUILD_ID=dontKillMe
daemonize nohup $SPLEBASE/bin/threadpoolworker.sh -d Y -p DEFAULT=5 >/dev/null 2>&1 &

It didn’t worked. Alternatively when I tried to run above command without nohup the jenkins job is running indefinitely. Its like I am stuck in an infinite loop.

Can anyone help me on this, I tried many options but didn’t none of it worked. It will be very helpful if someone could help me here.

You can use
JENKINS_NODE_COOKIE=dontKillMe nohup java -jar app.jar > nohup.out &
Reference: Jenkins : ProcessTreeKiller

How can I do the same thing on Windows machine?