抑制子进程的输出 [英] Suppress output of subprocess

查看:33
本文介绍了抑制子进程的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 subprocess 模块来控制一些通过 ssh 产生的进程.

I want to use the subprocess module to control some processes spawned via ssh.

通过搜索和测试,我发现这是有效的:

By searching and testing I found that this works:

import subprocess
import os
import time

node = 'guest@localhost'
my_cmd = ['sleep','1000']
devnull = open(os.devnull, 'wb')
cmd = ['ssh', '-t', '-t', node] + my_cmd
p = subprocess.Popen(cmd, stderr=devnull, stdout=devnull)


while True:
    time.sleep(1)
    print 'Normal output'

我提供的 -t -t 选项允许我终止远程进程,而不仅仅是 ssh 命令.但这也会扰乱我的程序输出,因为换行符不再有效,使其成为一个又长又难读的字符串.

The -t -t option I provide allows me to terminate the remote process instead of just the ssh command. But this, also scrambles my program output as newlines are no longer effective making it a long and hard to read string.

如何让ssh不影响python程序的格式?

How can I make ssh not affecting the formatting of the python program?

示例输出:

guest:~$ python2 test.py 
Normal output
             Normal output
                          Normal output
                                       Normal output
                                                    Normal output
                                                                 Normal output
                                                                              Normal output
(First ctrl-c)
Normal output
Normal output
Normal output
(Second ctrl-c)
^CTraceback (most recent call last):
  File "test.py", line 13, in <module>
    time.sleep(1)
KeyboardInterrupt

推荐答案

好的,现在输出清晰了.我不完全知道为什么,但是命令 ssh -t -t 将本地终端置于原始模式.无论如何它是有道理的,因为它旨在允许您直接在远程上使用curses程序(例如vi),在这种情况下,不应进行任何转换,即使是简单的\n-> \r\n 允许一个简单的新行将光标留在第一列.但是我在 ssh 文档中找不到关于此的参考.

Ok, the output is now clear. I do not exactly know why, but the command ssh -t -t puts the local terminal in raw mode. It makes sense anyway, because it is intended to allow you to directly use curses programs (such as vi) on the remote, and in that case, no conversion should be done, not even the simple \n -> \r\n that allows a simple new line to leave the cursor on first column. But I could not find a reference on this in ssh documentation.

它 (-t -t) 允许你杀死远程进程,因为原始模式让 Ctrl + C 被发送到远程而不是被处理由本地 tty 驱动.

It (-t -t) allows you to kill the remote process because the raw mode let the Ctrl + C to be sent to the remote instead of being processed by the local tty driver.

恕我直言,这是设计味道,因为您只使用 pty 分配的副作用将 Ctrl + C 传递给遥控器,而您却为另一边受苦效果是本地系统上的原始模式.您应该处理标准输入(stdinput = subprocess.PIPE)并在本地键盘上输入特殊字符或安装信号时明确发送 chr(3)执行此操作的 SIG-INT 处理程序.

IMHO, this is design smell, because you only use a side effect of the pty allocation to pass a Ctrl + C to the remote and you suffer for another side effect which is the raw mode on local system. You should rather process the standard input (stdinput = subprocess.PIPE) and explicitely send a chr(3) when you input a special character on local keyboard, or install a signal handler for SIG-INT that does it.

或者,作为一种解决方法,您可以在启动远程命令以重置本地终端后简单地使用类似 os.system("stty opost -igncr")(或更好的子进程等效项)之类的东西处于可接受的模式.

Alternatively, as a workaround, you can simply use something like os.system("stty opost -igncr") (or better its subprocess equivalent) after starting the remote command to reset the local terminal in an acceptable mode.

这篇关于抑制子进程的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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