Saltar al contenido

~ / archivo / stackoverflow / 37160402

Clean way of launching a shell script in background from Jenkins

StackOverflow · linux, bash, shell, jenkins · ayudé
  1. garci560 pregunta +30 2016-05-11 10:49

    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?

    • anthony sottile 2018-10-29 19:37 I'm nominating for reopen, the answers to this question are correct and the duplicate has none of the correct information
  2. Inian respuesta +36 2016-05-11 11:16

    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
    
    • Joshua 2018-02-13 17:56 If this doesn't work, try setting JENKINS_NODE_COOKIE=dontKillMe instead - devops.stackexchange.com/questions/1473/…
    • Serge 2019-07-01 14:38 FYI, you want to use JENKINS_NODE_COOKIE=dontKillMe for Pipeline projects, as BUILD_ID=dontKillMe does not work there. Source: wiki.jenkins.io/display/JENKINS/ProcessTreeKiller
    • ArtOfWarfare 2021-05-12 18:46 nohup didn't seem to output a file named nohup.out like I expected it to. I needed to include >scan.log 2>&1 just before the & so that I could actually check the output from this script later in the pipeline (or manually by browsing the file in the workspace.)
  3. user2671131 respuesta +9 2016-11-09 19:52

    I was having the exact same problem. I ended up fixing this by placing the following in the Jenkins execute shell box:

    BUILD_ID=dontKillMe ./grid.sh
    

    I moved the & inside the script file. Here's what the script looks like:

    #!/bin/bash
    java -jar selenium-server-standalone-3.0.1.jar -role hub &
    

    Hopefully this helps someone!

  4. GrueMaster respuesta +2 2019-03-12 00:27

    Interesting solutions. Has anyone tried screen?

    Here's how I run it (for RTL simulation using ModelSim-ASE):

    screen -S $testname -d -m -L bash -c 'cd build_sim ; make; make sim'
    echo Waiting for simulator ...
    

    I then wait for our simulator to get to a waiting state by watching the screenlog.0

    until ((`fgrep -c 'Ready for simulation' screenlog.0`)) ;do
        sleep 1
    done
    

    Then, when the other jobs that need the simulation are done running (or Jenkins detects a failure), in the post, run:

    screen -S $testname -X kill
    

    The only caveat is that if Jenkins somehow dies before the job cleans up, in theory you could have a simulator chewing up resources while waiting for something to happen.

    To break it down a little: With screen, '-S' assigns a title to the session (or addresses a running session with that title), '-d' runs detached, '-m' spawn a fork first (which hides from Jenkins watchful eyes), and -L turns on logging (which is also handy in case something in the simulator breaks - you now have a log file artifact with the output). The rest is simply bash inside of the screen session.

  5. yasin sula respuesta +1 2023-08-11 11:19

    I have implemented the solution as follows:

    stage('Start AdminServer') {
      sh 'export JENKINS_NODE_COOKIE=dontKillMe;export BUILD_ID=dontKillMe;/app/jenkins/scripts/applyPatch/applyPatch.sh startAdmin' 
    }
    
  6. Miguel Ortiz yo respuesta 2021-01-18 17:27

    I've solved a similar issue by executing:

    mycommand -parameters &> /dev/null &
    

    Somehow Jenkins was attached to the stdout of my script.

    So I tried redirecting the standard output/error to /dev/null using &> /dev/null then adding the process to background using & and solved the problem, Jenkins ended the build without killing the process spawned.

    PD: I tried adding an exit 0 at the end of the script, and it was executed, but that didn't free the Jenkins build, it kept attached to the subprocess output.

  7. msaba respuesta 2022-09-20 04:16

    your Jenkins command box should only contain ksh /path/to/script/script.sh

    your script file should look like this:

    #!/bin/bash
    command > output.log &
    

← todos los hilos de StackOverflow