Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I start an SSH session that executes a long-running command, what happens with Ctrl+C (SIGINT) handling?

I can see that the SSH session is closed, but I am not sure who gets the SIGINT first: is it...

  1. the remote long-running command? that is, (a) the signal handler in the remote command is called and stops the remote command, (b) the shell that spawned it detects that the command stopped, and stops as well (c) the remote sshd detects the shell stopped, so it closes the connection

    or

  2. the local ssh receives the signal, and closes the connection.

I think that (1) is happening, but want to make sure.

I am also uncertain about what happens with shell handling of SIGINTs in this case. For example, if I ...

ssh remote 'while true ; do sleep 1 ; date ; done'

and Ctrl+C, then the remote connection is dropped. Is there a way to run the remote command under a shell that will stay alive after Ctrl+C? That is, in this case, stop the loop and allow me to keep working on the remote shell?

share|improve this question
1  
A noninteractive ssh (ssh remote command, as opposed to ssh remote) will be killed (on the local side) by the SIGINT generated by typing ctrl-C. The remote side will probably (depends on the OS) stay running until it tries to read or write to the closed socket. If you want all your keystrokes, including ctrl-C, to be passed on to the remote, use ssh remote. –  Mark Plotnick Nov 21 '13 at 11:52

1 Answer 1

trap might help you:

and about your question. in my server Ctrl+C only closes connection. while is still running in remote server.

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.