如果没有从交互式终端运行,Python subprocess.communicate无法捕获runc容器的输出

问题描述 投票:0回答:1

我有一个运行子进程并捕获输出的脚本,但只有在交互式shell中运行它才有效,但如果从Jenkins运行则无效。

tst_subscriber = ["timeout", "-s", "KILL", str(timeout),"bash","-c", subsciber_executable]
tst_publisher = ["timeout","-s", "KILL", str(timeout),"bash","-c", publisher_executable]
kill_publisher = lambda process: subprocess.call(['docker-runc', 'delete', pub_name, "-f"])
kill_subscriber = lambda process: subprocess.call(['docker-runc', 'delete', sub_name, "-f"])

test_env = os.environ.copy()
# workaround for buffering problem which causes no captured output for python subprocesses
test_env["PYTHONUNBUFFERED"] = "1"

sub_pro = Popen(tst_subscriber, stdout=subprocess.PIPE, env=test_env)
pub_pro = Popen(tst_publisher, stdout=subprocess.PIPE, env=test_env)

timeout_sub = Timer(timeout, kill_subscriber, [sub_pro])
timeout_pub = Timer(timeout, kill_publisher, [pub_pro])

timeout_sub.start()
timeout_pub.start()
(output, err) = sub_pro.communicate()

print "Subscriber stdout:"
print output
print "Subscriber stderr:"
print err
python subprocess runc
1个回答
0
投票

由于几个开放的runc问题#1965 #1721,在config.json中设置terminal: true会导致管道问题。 Seeting terminal: false解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.