我试图将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
出于安全原因,sudo从TTY读取密码,而不是从标准输入读取密码。请尝试使用以下命令:
这将使sudo从标准输入中读取密码,除非在sudoers文件中指定了
requiretty
,在这种情况下,您必须模拟TTY。在顺便说一句,请注意sudo支持许多身份验证方法:password只是其中之一。特别是,sudo可能不会要求密码(例如,当使用
NOPASSWD
)时,因此至少要确保在向进程写入密码之前检查密码提示是否存在。在一般来说,考虑一下提供一个使用sudo和提升权限的程序是否会让用户感到高兴。在
相关问题 更多 >
编程相关推荐