【发布时间】:2017-06-12 09:42:30
【问题描述】:
我正在尝试将 stdout 和 stderr 写入文件并输入存储在字符串中的 sudo 提示的密码。尝试以下列方式在后台执行时,在 err 文件中出现损坏的管道错误。
cmd.py
def preexec_function():
import os
import signal
# Detaching from the parent process group
os.setpgrp()
# Explicitly ignoring signals in the child process
signal.signal(signal.SIGINT, signal.SIG_IGN)
cmd = "python pexecutor.py"
p = Popen(cmd, close_fds=True, stdout=None, stderr=None, stdin=None,
preexec_fn=preexec_function)
执行程序.py
from subprocess import Popen, PIPE
import os
command="sudo yum -y install postgresql.x86_64"
stdin_str="myrootpwd"
std_out_file = open("out.txt", 'a+')
std_err_file = open("err.txt", 'a+')
process = Popen(command, stdout=std_out_file, stderr=std_err_file,
stdin=PIPE)
import time
time.sleep(5)
pwd = b'{}'.format(stdin_str + str(os.linesep))
process.stdin.write(pwd)
process.stdin.flush()
data = process.communicate()
得到错误:
Traceback (most recent call last):
File "pexecutor.py", line 10, in execute
process.stdin.write(pwd)
IOError: [Errno 32] Broken pipe
操作系统:CentOS
Python 版本:2.7.5
【问题讨论】:
标签: python subprocess ioerror