How to send SIGINT (Ctrl-C) to current remote process over SSH (without -t option)

10,846

Solution 1

If you use ssh to start a process without a PTY on a remote system, then as far as I can tell, there's no way to signal the remote process through that ssh session.

The SSH protocol has a message to send a signal to the remote process. However, you're probably using OpenSSH for either the client or the server or both, and OpenSSH doesn't implement the signal message. So the OpenSSH client can't send the message, and the OpenSSH server won't act on it.

There is an SSH extension to send a "break" message which is supported by OpenSSH. In an interactive session, the OpenSSH client has an escape sequence that you can type to send a break to the server. The OpenSSH server handles break messages by sending a break to the PTY for the remote session, and unix PTYs will normally treat a break as a SIGINT. However, breaks are fundamentally a TTY concept, and none of this will work for remote sessions which don't have a PTY.

Solution 2

Short answer:

ssh -t fs "stty isig intr ^N -echoctl ; trap '/bin/true' SIGINT; sleep 1000; echo f" > foo

and stop the program by CTRL+N.

Long explanation:

1.You must use stty option intr to change your server or local interrupt character to not collide with each other.In the command above I've changed the server interrupt character to CTRL+N. You can change your local interrupt character and leave the server's one without any changes.

2.If you don't want the interrupt character to be in your output (and any other control character) use stty -echoctl.

3.You must assure that control characters are switched on on the server bash invoked by sshd . If you don't you can end up with processes still hanging around after you logout. stty isig

4.You actually catch SIGINT signal by trap '/bin/true' SIGINT with empty statement. Without the trap you will not have any stdout after SIGINT signal on your end.

But without -t option that cannot be quit with a signal to the process that runs over the terminal.

Share:
10,846
Manuel Schmidt
Author by

Manuel Schmidt

I'm a passionate IT consultant

Updated on June 11, 2022

Comments

  • Manuel Schmidt
    Manuel Schmidt almost 2 years

    I need to send a SIGINT to the remote process running in foreground in an SSH session.

    The SSH session is already established, so I cannot use option of starting it with (as described in How to send SIGINT to a remote process over SSH?)

    ssh -t user@host
    

    I know I could open a second ssh session and kill the process or close the current ssh session, but I want to avoid that and do it directly throw the current session (if this is possible).

  • Ahmed Masud
    Ahmed Masud almost 7 years
    OP is specifically asking to not use -t
  • Harini
    Harini almost 7 years
    The last statement clearly shows that can't be achieved
  • Guss
    Guss over 5 years
    Hot off of the presses: There's a bug report to add "signal" message support to OpenSSH and according to reports (which I have not verified), OpenSSH 7.9 (released Oct 2018) and onwards supports receiving signals (though not sending from the client, that may require additional implementation) bugzilla.mindrot.org/show_bug.cgi?id=1424