As I Doothings

Navigation

Recent site activity

Home‎ > ‎

ssh remote with nohup for anyone

I found [for myself] some strange exceptions to how remote ssh command execution normally works. At least, I normally know remote execution with

machine_A$ ssh user@machine_B "command <arg1> <arg2> ..."

allows you to capture the output of a command running on machine_B at the standard output of a terminal at machine_A. But the ssh session will run until the command finishes running. The trouble is, what if you want to be done with it? I wanted to run some jobs in parallel on a few machines:


machines=( machine1.dom machine2.dom machine3.dom )

for machine in ${machines[*]}
do
    ssh user@${machine} "command <arg1> <arg2> ..."
done


But this will hit these systems in series, waiting for each command to finish first. The natural(?) response is to use an &, but strangely enough, this works not.

Using the insight from [1], all of the output error and regular output still goes to STDOUT and STDERR at machine_A when issuing

machine_A$ ssh user@machine_B "command <arg1> <arg2> ... &"

And nohup doesn't help either:

machine_A$ ssh user@machine_B "nohup command <arg1> <arg2> ... &"

The ssh connection is still open. Inspecting the nohup manpage, I was disappointed, because there's no mention of how to redirect output to somewhere other than a file called nohup.out in the current working directory. I expected all messages to go to nohup.out. But trying to redirect both errors and standard output messages works great:

machine_A$ ssh user@machine_B "command <arg1> <arg2> ... >/dev/null >nohup.out &"

Yes, even without using nohup. You can apply nohup just to make the command immune to SIGHUP.


References

[1] Catalin Petrescu, Jim Horwath, http://www.derkeiler.com/Mailing-Lists/securityfocus/Secure_Shell/2005-10/0007.html