使用Python将信号发送到远程进程 [英] Send signals to remote process with Python

查看:72
本文介绍了使用Python将信号发送到远程进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两台机器,其中一台具有脚本 wait_for_signal.sh ,第二台具有名为 controller.py 的脚本.每个脚本的代码如下所示.

There are two machines, where one has a script wait_for_signal.sh, and the second one has a script named controller.py. The code of each script is shown below.

controller.py 的目的是生成一个子进程,该子进程通过 ssh 调用 wait_for_signal.sh 脚本.当控制器需要退出时,它需要向运行 wait_for_signal.sh 的远程进程发送中断.

The purpose of the controller.py is to spawn a subprocess that calls the wait_for_signal.sh script through ssh. When the controller needs to exit it needs to send an interrupt to the remote process that runs wait_for_signal.sh.

wait_for_signal.sh

#!/bin/bash
trap 'break' SIGINT
trap 'break' SIGHUP

echo "Start loop"

while true; do
  sleep 1
done

echo "Script done"

controller.py

import os
import signal
import subprocess

remote_machine = user@ip
remote_path = path/to/script/

remote_proc = subprocess.Popen(['ssh', '-T', remote_machine, 
                               './' + remote_path + 'wait_for_signal.sh'], 
                               shell=False, stdout=subprocess.PIPE, 
                               stderr=subprocess.PIPE)

# do other stuff

os.kill(remote_proc.pid, signal.SIGINT)

当前,信号发送到在本地计算机而非远程计算机上启动ssh连接的进程.这将导致本地进程停止,但远程进程继续执行.

Currently, the signal send is to the process that started the ssh connection on the local machine and not the remote machine. This causes the local process to stop but the remote process continues to execute.

ssh如何工作,停止时它将发送给远程计算机什么类型的信号?如何将适当的信号发送到由ssh连接启动的远程进程?

How does ssh work and what type of signals does it send to the remote machine when it is stopped? How can I send the appropriate signal to the remote process, which was started by the ssh connection?

推荐答案

您正在使用 -T 选项调用 ssh ,这意味着它不会分配远程会话的PTY(伪TTY).在这种情况下,无法通过该ssh会话向远程进程发出信号.

You're invoking ssh with the -T option, meaning that it won't allocate a PTY (pseudo-TTY) for the remote session. In this case, there's no way to signal the remote process through that ssh session.

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

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 as far as I can tell, 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 服务器通过向远程会话的 PTY 发送中断来处理中断消息,而 unix PTY 通常会将中断视为 SIGINT.但是,从根本上讲,中断是TTY的概念,对于没有PTY的远程会话,这些都不起作用.

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.

我可以想到两种方法来完成您想要的事情:

I can think of two ways to do what you want:

  1. 使用 -tt 参数而不是 -T 调用 ssh .这将导致ssh请求远程会话的TTY.通过TTY运行远程进程将使其表现得像交互式运行一样.终止本地ssh进程应导致远程进程收到SIGHUP.将 Ctrl - C 写入本地ssh进程的标准输入应该会导致远程进程接收到SIGINT.

  1. Invoke ssh with the -tt parameter instead of -T. This will cause ssh to request a TTY for the remote session. Running the remote process through a TTY will make it act like it's running interactively. Killing the local ssh process should cause the remote process to receive a SIGHUP. Writing a Ctrl-C to the local ssh process's standard input should cause the remote process to receive a SIGINT.

打开另一个与远程主机的ssh会话,并使用 killall 或其他命令来发出您要发出信号的信号.

Open another ssh session to the remote host and use killall or some other command to signal the process that you want to signal.

这篇关于使用Python将信号发送到远程进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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