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).
Thus, if you want to send signals SIGINT, SIGQUIT and SIGKILL, use INT, QUIT and KILL, respectively. If the signal name is missing, the default SIGTERM is used. Following the signal name is a list of process IDs to which the signal will be sent.
Sending Signals Using The Keyboard Ctrl-C. Pressing this key causes the system to send an INT signal ( SIGINT ) to the running process. By default, this signal causes the process to immediately terminate.
The default signal is SIGTERM. kill is a built-in shell command. In the tcsh shell, kill [-signal] %job|pid … sends the specified signal (or if none is given, the TERM (terminate) signal) to the specified jobs or processes.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With