解决SSH不转发信号 [英] Work around ssh does not forward signal

查看:69
本文介绍了解决SSH不转发信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决ssh无法转发SIGTERM信号的问题?

我希望print-signal.py终止,ssh root@localhost进程终止:

ssh root@localhost /root/print-signal.py

不幸的是,只有ssh进程本身会获得信号,而不是远程命令(print-signal.py).远程命令不会终止:-(

由于openssh不会将SIGTERM转发给远程命令,因此我正在寻找解决方法.

如果ssh root@localhost ...终止,如何终止print-signal.py?

这是针对以下问题的后续问题:通过ssh转发SIGTERM

解决方案

免责声明:以下答案不是SIGTERM,而是SIGINT.这不是监督导致的问题的答案.

您正在观察的问题是由于缺少tty,该tty应该可以控制您尝试运行的进程.如果没有tty可用,则ssh无法将信号发送到该进程.在ssh命令中使用选项-t时,它将强制进行伪终端分配,这使得可以通过ssh发送信号:

ssh -t root@localhost /root/print-signal.py

man ssh -t强制伪终端分配.这可用于在远程计算机上执行任意基于屏幕的程序,这可能非常有用,例如实施菜单服务时.多个-t选项强制tty分配,即使ssh没有本地tty.

基于unix.stackexchange的吉尔斯给出了一个很好的解释.

在这里,您可以看到它的工作原理:

[terminal 1]% ssh server ./print_signal.py

在另一个终端上,您会看到print_signal.pyssh下的PID=26992上运行,其中PID=26991没有tty(username@notty)

[terminal 2]% ssh server ps -f                                                                                                                                                                                                            
UID        PID  PPID  C STIME TTY          TIME CMD
username 26991 26989  0 17:06 ?        00:00:00 sshd: username@notty
username 26992 26991  0 17:06 ?        00:00:00 python ./print_signal.py
username 27347 27345  0 17:07 ?        00:00:00 sshd: username@notty
username 27348 27347  0 17:07 ?        00:00:00 ps -f

使用kill或 CTRL - C 杀死ssh进程后,该进程仍处于活动状态,但现在在/sbin/init(PPID=1)下运行

[terminal 2]% ssh server ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
username 26992     1  0 17:06 ?        00:00:00 python ./print_signal.py
username 27453 27451  0 17:08 ?        00:00:00 sshd: username@notty
username 27454 27453  5 17:08 ?        00:00:00 ps -f

使用-t标志可以很好地杀死另一端的进程:

[terminal 1]% ssh -t server ./print_signal.py

在另一个终端上,您会看到print_signal.pyssh下的PID=39277上运行,并且PID=39276绑定到tty(username@pts/10)

[terminal 2]% ssh server ps -U username -f
UID        PID  PPID  C STIME TTY          TIME CMD
username 39276 39274  0 17:22 ?        00:00:00 sshd: username@pts/10
username 39277 39276  1 17:22 pts/10   00:00:00 python ./print_signal.py
username 39317 39314  0 17:22 ?        00:00:00 sshd: username@notty
username 39318 39317  5 17:22 ?        00:00:00 ps -U username -f

杀死ssh进程

[terminal 1]% ssh -t server ./print_signal.py
My PID: 39277
^CCaught signal SIGINT (2), exiting.
Connection to server closed

该进程现在显然已在另一台服务器上终止

[terminal 2]% ssh server ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
username 39768 39765  0 17:26 ?        00:00:00 sshd: username@notty
username 39769 39768  6 17:26 ?        00:00:00 ps -U username -f

How can I work around the problem that ssh does not forward the SIGTERM signal?

I want print-signal.py to terminate of the ssh root@localhost process terminates:

ssh root@localhost /root/print-signal.py

Unfortunately only the ssh process itself gets the signal, not the remote command (print-signal.py). The remote command does not terminate :-(

Since openssh does not forward the SIGTERM to the remote command, I am searching for a work-around.

How to terminate print-signal.py if ssh root@localhost ... terminates?

This is a follow-up question to: Forwarding SIGTERM over ssh

解决方案

Disclaimer: the answer below is not for SIGTERM, but SIGINT. This is not an answer to the question due to oversight.

The problem you are observing is due to a missing tty which should be in control of the process you try to run. If there is no tty available, ssh is unable to send the signals to the process. When you use the option -t to the ssh command, it will force pseudo-terminal allocation which makes it possible to send signals over ssh :

ssh -t root@localhost /root/print-signal.py

man ssh -t Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

A very nice explanation how and why is given by Giles on unix.stackexchange.

Here you see how it works :

[terminal 1]% ssh server ./print_signal.py

On an other terminal you see then that print_signal.py is running on PID=26992 under the ssh with PID=26991 without a tty (username@notty)

[terminal 2]% ssh server ps -f                                                                                                                                                                                                            
UID        PID  PPID  C STIME TTY          TIME CMD
username 26991 26989  0 17:06 ?        00:00:00 sshd: username@notty
username 26992 26991  0 17:06 ?        00:00:00 python ./print_signal.py
username 27347 27345  0 17:07 ?        00:00:00 sshd: username@notty
username 27348 27347  0 17:07 ?        00:00:00 ps -f

After killing the ssh process with kill or CTRL-C, the process is still active but runs now under /sbin/init (PPID=1)

[terminal 2]% ssh server ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
username 26992     1  0 17:06 ?        00:00:00 python ./print_signal.py
username 27453 27451  0 17:08 ?        00:00:00 sshd: username@notty
username 27454 27453  5 17:08 ?        00:00:00 ps -f

using the -t flag nicely kills the process on the other side:

[terminal 1]% ssh -t server ./print_signal.py

On an other terminal you see then that print_signal.py is running on PID=39277 under the ssh with PID=39276 bound to a tty (username@pts/10)

[terminal 2]% ssh server ps -U username -f
UID        PID  PPID  C STIME TTY          TIME CMD
username 39276 39274  0 17:22 ?        00:00:00 sshd: username@pts/10
username 39277 39276  1 17:22 pts/10   00:00:00 python ./print_signal.py
username 39317 39314  0 17:22 ?        00:00:00 sshd: username@notty
username 39318 39317  5 17:22 ?        00:00:00 ps -U username -f

After killing the ssh process

[terminal 1]% ssh -t server ./print_signal.py
My PID: 39277
^CCaught signal SIGINT (2), exiting.
Connection to server closed

The process is now clearly terminated on the other server

[terminal 2]% ssh server ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
username 39768 39765  0 17:26 ?        00:00:00 sshd: username@notty
username 39769 39768  6 17:26 ?        00:00:00 ps -U username -f

这篇关于解决SSH不转发信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆