Jenkins SSH remote process is getting killed as soon as the Jenkins SSH plugin returns back

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Jenkins SSH remote process is getting killed as soon as the Jenkins SSH plugin returns back



Jenkins version: 1.574



I created a simple job which performs the following:



Using "Execute shell script on remote host using SSH" as one of the BUILD steps, I'm just calling a shell script. This shell script performs stop and start operations on Tomcat to restart an application on the target machine.



I have a valid username, password, port defined for the target SSH server in Jenkins Global settings.



I saw this behavior that when I run a Jenkins job and call the restart script (which gets the application name as parameter $1), it works fine, but as soon as "Execute shell script on remote host using SSH" step completes, I see the new process dies on the remote/target application server.



If I run the script from the target/remote server itself, everything works fine and the new process/PID remains live forever, but running the same script from Jenkins, though I don't see any errors and everything works as expected, the new process dies as soon as the above mentioned SSH step is complete and control comes back to the next BUILD step in Jenkins job OR the Jenkins job is complete.



I saw a few posts/blogs and tried setting: BUILD_ID=dontKillMe in the Jenkins job (in various places i.e. Prepare Environment variables and also using Inject Environment variables...). When the job's particular build# is complete, I can see Environment Variables for that build# does say BUILD_ID=dontKillMe as its value (instead of the default Timestamp tag value).



I tried putting nohup before calling the restart script, i.e.,


nohup restart_tomcat.sh "$app"



I also tried:


BUILD_ID=dontKillMe nohup restart_tomcat.sh "$app"



This doesn't give any error and creates a nohup.out file on the remote server (but I'm not worried about it as the restart_tomcat.sh script itself creates its own LOG file which I'm "cat"ing after the restart_tomcat.sh script is complete. cat'ing on the log file is performed using another "Execute shell script on remote host using SSH" build step, and it successfully shows the log file created by the restart script).


nohup.out


restart_tomcat.sh


restart_tomcat.sh



I don't know what I'm missing at this point, but as soon as the restart_tomcat.sh step is complete, the new PID/process on the remote/target server dies.


restart_tomcat.sh



How can I fix this?




3 Answers
3



I've been through this myself.



On my first iteration, before I knew about Jenkins ProcessTreeKiller, I ended up just daemonizing Tomcat. The Apache Tomcat documentation includes a section on running as a daemon.



You can also try disabling the ProcessTreeKiller for your whole Jenkins instance, if it's relatively small (read the first link for information).



The BUILD_ID=dontKillMe should be passed to the shell, and therefore it should be in your command line, not in Jenkins global configuration or job parameters.


BUILD_ID=dontKillMe



BUILD_ID=dontKillMe restart_tomcat.sh "$app" should have worked without problems.


BUILD_ID=dontKillMe restart_tomcat.sh "$app"



You can also try nohup restart_tomcat.sh "$app" & with the & at the end.


nohup restart_tomcat.sh "$app" &


&





It'll work in case you do the above in an "Execute shell" section, I'm running it using "Execute shell script on remote host using SSH" section, tried BUILD_ID=dontKillMe restart_tomcat.sh "$app", didn't stop the process from being killed though. Thanks.
– Arun Sangal
Aug 26 '14 at 20:51






Did you try nohup with & at the end?
– Slav
Aug 27 '14 at 13:36


nohup


&



My solution (it worked after trying everything else) in Ubuntu 14.04 (Trusty Tahr) (Amazon AWS - Amazon EC2), Jenkins 1.601:



Exec command: (setsid COMMAND < /dev/null > /dev/null 2>&1 &);



Exec in PTY: DISABLED


// Example COMMAND=socat TCP4-LISTEN:1337,fork TCP4:127.0.0.1:1338



I created this Transfer as my last one.


#!/bin/ksh
export BUILD_ID=dontKillMe



I added the above line to the start of my script and the issue was resolved.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard