python子进程标准写入(pwd)IOError:[Errno 32]pip损坏

2024-09-30 18:25:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将stdout和stderr写入文件,并输入sudo提示符的密码,该密码存储在字符串中。以下面的方式在后台执行时获取err文件中的断管错误。在

在命令行在

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在

^{pr2}$

获取错误:

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


Tags: 文件thepyimportnone密码signalos
1条回答
网友
1楼 · 发布于 2024-09-30 18:25:39

出于安全原因,sudo从TTY读取密码,而不是从标准输入读取密码。请尝试使用以下命令:

sudo  stdin yum -y install postgresql.x86_64

这将使sudo从标准输入中读取密码,除非在sudoers文件中指定了requiretty,在这种情况下,您必须模拟TTY。在


顺便说一句,请注意sudo支持许多身份验证方法:password只是其中之一。特别是,sudo可能不会要求密码(例如,当使用NOPASSWD)时,因此至少要确保在向进程写入密码之前检查密码提示是否存在。在

一般来说,考虑一下提供一个使用sudo和提升权限的程序是否会让用户感到高兴。在

相关问题 更多 >