Python通过ssh终止进程 [英] Python Terminating a process over ssh

查看:46
本文介绍了Python通过ssh终止进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 ssh 终止进程.现在,当直接终止进程时,请参见 init 中注释掉的行,该进程在远程主机上终止.当然,我不想立即终止,而是在最后终止所有远程进程.

I am trying to terminate a process over ssh. Now, when terminating the process directly, see commented out line in init, the process gets terminated on the remote Host. Of course, I don't want to immediately terminate, instead terminate all remote processes at the end.

根据 subprocess.Popen 中的选项 '-t',当我尝试在示例末尾的循环中终止时会出现不同的结果:

Depending on the option '-t' in subprocess.Popen, different outcomes appear when I try to terminate in the loop at the end of the example:

如果-t"设置,则进程终止.

If '-t' is not set, the process is not terminated.

如果设置了-t",进程将终止,但我的终端会隐藏任何输入,任何输入都会移动输入行.

If '-t' is set, the process is terminated, but my terminal hides any input and any enter moves the input line around.

现在,如果没有 '-t' 选项,为什么进程不会在最后的循环处终止?

Now, why doesn't, without the '-t' option, the process terminate at the loop at the end?

serverThreads = []
serverscripts = []
serverscripts.append("script1")
serverscripts.append("script2")

class ServerCaller(threading.Thread):
def __init__(self, script):
    threading.Thread.__init__(self)
    self.script = script
    self.ssh = subprocess.Popen(["ssh", '-t' , username + dataServer, self.script])
    #if I terminate here without '-t' the process on the remote Host is terminated
    #self.term()
def term(self):
    self.ssh.terminate()

for ss in serverscripts:
    serverThreads.append(ServerCaller(ss))

#if I terminate here without '-t' the process on the remote Host is NOT terminated
#if I terminate here with '-t' the process on the remote Host is terminated,
#but my terminal is screwed up
for t in serverThreads:
    t.term()

推荐答案

当您使用 ssh 在远程系统上调用命令,并且您使用 -t为远程命令请求一个 TTY,然后远程 ssh 服务器将分配一个 TTY 并以 TTY 作为其控制 TTY 启动远程命令.

When you use ssh to invoke a command on a remote system, and you use -t to request a TTY for the remote command, then the remote ssh server will allocate a TTY and launch the remote command with the TTY as its controlling TTY.

当您使用 ssh 在没有 TTY 的情况下调用远程命令时,远程 SSH 服务器将创建一组管道并使用这些管道作为命令的标准输入、输出、和错误.

When you use ssh to invoke a remote command without a TTY, then the remote SSH server will create a set of pipes and launch the remote command with those pipes as the command's standard input, output, and error.

当您终止本地 ssh 实例时,这会关闭与服务器的 SSH 连接.发生这种情况时,远程服务器不会明确尝试终止您之前启动的远程进程.远程服务器只是关闭与远程进程的连接——TTY 或管道集.

When you terminate your local ssh instance, this closes the SSH connection to the server. When this happens, the remote server doesn't explicitly try to kill the remote process that you previously started. The remote server just closes its connection to the remote process--either the TTY or the set of pipes.

如果远程进程是用 TTY 启动的(因为你指定了 -t),那么关闭 TTY 将导致远程进程接收到一个 SIGHUP 信号.SIGHUP 将导致进程终止,除非该进程特别安排捕获或忽略该信号.

If the remote process was started with a TTY (because you specified -t), then closing the TTY will cause the remote process to receive a SIGHUP signal. SIGHUP will cause a process to terminate, unless the process specifically arranges to catch or ignore the signal.

如果远程进程是在没有 TTY 的情况下启动的,那么关闭进程的标准输入/输出/错误不会立即生效.该进程不会收到 SIGHUP 或其他信号.如果进程试图从标准输入读取,它会得到文件结束条件.如果它尝试写入 stdout/stderr,它将获得一个 SIGPIPE(默认情况下会终止进程).如果它不访问 stdin/stdout/stderr,那么这些事情都不会发生,进程可以无限期地继续运行.

If the remote process was started without a TTY, then closing the process's standard input/output/error has no immediate effect. The process won't receive a SIGHUP or other signal. If the process tries to read from standard input, it'll get an end-of-file condition. If it tries to write to stdout/stderr, it'll get a SIGPIPE (which terminates the process by default). If it doesn't access stdin/stdout/stderr, then none of these things will happen and the process could continue running indefinitely.

这篇关于Python通过ssh终止进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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