bash 如何通过 SSH 将 SIGINT (Ctrl-C) 发送到当前远程进程(不带 -t 选项)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44348083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:12:00  来源:igfitidea点击:

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

linuxbashssh

提问by Manuel Schmidt

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

我需要向在 SSH 会话中在前台运行的远程进程发送一个 SIGINT。

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 会话已经建立,因此我无法使用启动它的选项(如如何通过 SSH 将 SIGINT 发送到远程进程中所述?

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).

我知道我可以打开第二个 ssh 会话并终止进程或关闭当前的 ssh 会话,但我想避免这种情况并直接抛出当前会话(如果可能的话)。

回答by Kenster

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.

如果您在远程系统上使用 ssh 在没有 PTY 的情况下启动进程,那么据我所知,无法通过该 ssh 会话向远程进程发送信号。

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.

SSH 协议具有向远程进程发送信号消息。但是,您可能将 OpenSSH 用于客户端或服务器或两者,并且 OpenSSH 不实现信号消息。因此 OpenSSH 客户端无法发送消息,OpenSSH 服务器也不会对其进行操作。

There is an SSH extension to send a "break" messagewhich issupported by OpenSSH. In an interactive session, the OpenSSH client has an escape sequencethat 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.

有一个SSH扩展发送“中断”的消息,其通过OpenSSH的支持。在交互式会话中,OpenSSH 客户端有一个转义序列,您可以键入该序列以向服务器发送中断。OpenSSH 服务器通过向远程会话的 PTY 发送中断来处理中断消息,而 unix PTY 通常会将中断视为 SIGINT。但是,中断从根本上说是一个 TTY 概念,对于没有 PTY 的远程会话,这些都不起作用。

回答by Harini

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.

并按 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.

1.您必须使用stty选项intr来更改您的服务器或本地中断字符,以免相互冲突。在上面的命令中,我将服务器中断字符更改为CTRL+N。您可以更改本地中断字符,而无需任何更改就保留服务器的中断字符。

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

2.如果您不希望中断字符出现在您的输出(和任何其他控制字符)中,请使用 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

3.您必须确保在 sshd 调用的服务器 bash 上打开了控制字符。如果不这样做,您可能会在注销后最终发现进程仍然存在。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.

4.您实际上是通过带有空语句的trap '/bin/true' SIGINT 来捕获SIGINT 信号。如果没有陷阱,您将不会在 SIGINT 信号之后有任何标准输出。

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

但是如果没有 -t 选项,则无法通过向在终端上运行的进程发出信号来退出。