简体   繁体   English

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

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

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会话,但我想避免这种情况并直接抛出当前会话(如果可能的话)。

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 . SSH协议有一条消息,用于向远程进程发送信号 However, you're probably using OpenSSH for either the client or the server or both, and OpenSSH doesn't implement the signal message. 但是,您可能将OpenSSH用于客户端或服务器或两者,并且OpenSSH不实现信号消息。 So the OpenSSH client can't send the message, and the OpenSSH server won't act on it. 因此OpenSSH客户端无法发送消息,OpenSSH服务器也不会对其进行操作。

There is an SSH extension to send a "break" message which is supported by OpenSSH. 有一个SSH扩展发送“中断”的消息 由OpenSSH的支持。 In an interactive session, the OpenSSH client has an escape sequence that you can type to send a break to the server. 在交互式会话中,OpenSSH客户端具有一个转义序列 ,您可以键入该转义序列以向服务器发送中断。 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. OpenSSH服务器通过向远程会话的PTY发送中断来处理中断消息,而unix PTY通常将中断视为SIGINT。 However, breaks are fundamentally a TTY concept, and none of this will work for remote sessions which don't have a PTY. 但是,休息时间基本上是TTY概念,并且这些都不适用于没有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. 并按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. 1.您必须使用stty选项intr来更改服务器或本地中断字符,以免相互冲突。在上面的命令中,我已将服务器中断字符更改为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. 2.如果您不希望中断字符出现在您的输出(以及任何其他控制字符)中,请使用stty -echoctl。

3.You must assure that control characters are switched on on the server bash invoked by sshd . 3.您必须确保在sshd调用的服务器bash上打开控制字符。 If you don't you can end up with processes still hanging around after you logout. 如果不这样做,您最终可能会在注销后仍然处于闲置状态。 stty isig stty isig

4.You actually catch SIGINT signal by trap '/bin/true' SIGINT with empty statement. 4.你实际上通过陷阱'/ bin / true'SIGINT用空语句捕获SIGINT信号。 Without the trap you will not have any stdout after SIGINT signal on your end. 如果没有陷阱,您在结束SIGINT信号后将不会有任何标准输出。

But without -t option that cannot be quit with a signal to the process that runs over the terminal. 但是没有-t选项,不能通过终端上运行的进程发出信号退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM