Using SSH (Scripts, Plugins, etc) to start processes

jenkins, jenkins-plugins

Solution

What probably happens in that the process when spawned (even with the &) is consuming the same input and output as your ssh connection. Jenkins is waiting for these pipes to be emptied before the job closes, thus waits for the processes to exit. You could verify that by killing your processes and you will see that the jenkins job terminates.

Dissociating outputs and starting the process remotely

There are multiple solutions to your problem:

(preferred) use proper daemon control tools. Your target platform probably has a standard way to manage those services, e.g. init.d scripts. Note, when writing init.d scripts, make sure you detach the process in the background AND ensure the input/output of the daemon are detached from the shell that starts them. There are several techniques, like like http://www.unix.com/man-page/Linux/8/start-stop-daemon/ tools, daemonize, daemontools or something like the shell script described under https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+as+a+Unix+daemon (take note of the su -s bin/sh jenkins -c "YOUR COMMAND; ...disown" etc). I also list some python specific techniques under.

ssh server 'program < /dev/null > /dev/null 2>&1 &'

or

ssh server 'program < /dev/null >> logfile.log 2>&1 &' if you want to have a crude output management (no log rotation, etc...)

potentially using setsid (I haven't tried) https://superuser.com/questions/172043/how-do-i-fork-a-process-that-doesnt-die-when-shell-exits . In my quick tests I wasn't able to get it to work though...

Python daemons

The initial question was focused on SSH, so I didn't fully described how to run the python process as daemon. This is mostly covered in other techniques:

- with start-stop-daemon: start-stop-daemon and python

- with upstart on ubuntu: Run python script as daemon at boot time (Ubuntu)

- some more python oriented approaches:

- How to make a Python script run like a service or daemon in Linux

- Can I run a Python script as a service?

Problem

I'm trying to finish a remote deployment by restarting the two processes that make my Python App work. Like so ``` process-one & process-two & ``` I've tried to "Execute a Shell Script" by doing this ``` ssh -i ~/.ssh/id_... user@xxx.xxx ./startup.sh ``` I've tried using the Jekins SSH Plugin and the Publish Over SSH Plugin and doing the same thing. All of the previous steps, stopping the processes, restarting other services, pulling in new code work fine. But when I get to the part where I start the services. It executes those two lines, and none of the Plugins or the Default Script execution can get off of the server. They all either hang until I restart Jekins or time out int he case of the Publish Over SSH plugin. So my build either requires a restart of Jenkins, or is marked unstable. Has anyone had any success doing something similar? I've tried ``` nohup process-one & ``` But the same thing has happened. It's not that the services are messing up either, because they actually start properly, it's just that Jenkins doesn't seem to understand that. Any help would be greatly appreciated. Thank you.

Original source

Related problems