Saturday, October 4, 2014

Managing server-side ssh commands

When running a long-running command over ssh, sometimes I need that command to be aware of the client connection and to exit with SIGINT on disconnect.

Stdin and stdout are the best ways to detect an active connection. (Other alternatives require port forwarding, or log trolling.)

This is the best way I have found to do this. You want something on the server side that attempts to read stdin and then kills the process group when that fails, but you also want a stdin on the client side that blocks until the server side process is done and will not leave lingering processes like <(sleep infinity) might.