J'essaie d'écrire stdout et stderr dans un fichier et d'entrer le mot de passe pour l'invite sudo qui est stockée dans une chaîne. Obtenir l'erreur de tuyau cassé dans le fichier err en essayant de l'exécuter en arrière-plan en dessous.Sous-processus python stdin.write (pwd) IOError: [Errno 32] Tube rompu
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)
pexecutor.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()
Obtenir l'erreur:
Traceback (most recent call last):
File "pexecutor.py", line 10, in execute
process.stdin.write(pwd)
IOError: [Errno 32] Broken pipe
OS: CentOS
Version Python: 2.7.5