When using SSH to send a command to a remote server, if the SSH connection dies the process keeps running. Are there any ways to have it kill the child processes if the SSH connection ends?

Example:

root@local:~# ssh root@server sleep 100 &
[2] 15762

root@local:~# kill 15762
[2]+  Stopped                 ssh root@server sleep 100

After running the above, the sleep command is still running on the remote host.

Any ideas?

link|improve this question
If it's any consolation, pretty much any task that writes to stdout will die due to a SIGPIPE (unless stdout is redirected on the remote host). – jdizzle Mar 10 '10 at 16:31
feedback

3 Answers

Don't run it in the background in the first place. Thus, when the ssh conn dies, the process will be killed as well.

link|improve this answer
feedback

jdizzle's comment gave me an idea: run your commands inside a script and handle signals with "trap" (http://gd.tuwien.ac.at/linuxcommand.org/wss0160.php)

would that do?

cheers, eddy

link|improve this answer
feedback

Log back in through SSH , run a ps -ef | grep commandname and kill the process?

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.