Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|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

3 Answers 3

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

share|improve this answer

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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