简体   繁体   English

禁止子流程的输出

[英]Suppress output of subprocess

I want to use the subprocess module to control some processes spawned via ssh. 我想使用子进程模块来控制通过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'

The -t -t option I provide allows me to terminate the remote process instead of just the ssh command. 我提供的-t -t选项允许我终止远程进程,而不仅仅是ssh命令。 But this, also scrambles my program output as newlines are no longer effective making it a long and hard to read string. 但是,这也扰乱了我的程序输出,因为换行符不再有效,这使得它变得很长且很难读取字符串。

How can I make ssh not affecting the formatting of the python program? 如何使ssh不影响python程序的格式?

Sample output: 样本输出:

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

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. 我不完全知道为什么,但是命令ssh -t -t将本地终端置于原始模式。 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. 无论如何,这是有道理的,因为它旨在允许您直接在远程上使用curses程序(例如vi),在这种情况下,不应该执行任何转换,即使是简单的\\n > \\r\\n允许简单的新行将光标留在第一列。 But I could not find a reference on this in ssh documentation. 但是我在ssh文档中找不到对此的参考。

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. 它( -t -t )允许您终止远程进程,因为原始模式使Ctrl + C可以发送到远程,而不是由本地tty驱动程序处理。

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. 恕我直言,这是设计气味 ,因为您仅使用pty分配的副作用将Ctrl + C传递到远程,并且遭受另一种副作用,这是本地系统上的原始模式。 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. 您应该处理标准输入( stdinput = subprocess.PIPE ),并在本地键盘上输入特殊字符时显式发送chr(3) ,或者为执行此操作的SIG-INT安装信号处理程序。

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. 另外,作为一种解决方法,您可以在启动远程命令以可接受的方式重置本地终端之后,简单地使用os.system("stty opost -igncr") (或更优的子过程)。

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

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