[英]Output of running code in Python shell is different from running script in IDLE
[英]No output from process running in background from shell script
我写了一个小脚本来测试Docker容器中的某些服务器。
#!/bin/bash
echo started
python /opt/server.py &
nginx -g 'daemon off;'
python服务器写了一些我们需要查看的信息,但是当我运行docker run -P image
或运行docker run -dP image
并检查docker日志时,它不会显示。
python服务器使用print
和sys.stdout.write
。 都没有出现我尝试重定向输出python /opt/server.py >&1
和/ dev / stdout。 两者都不起作用。 来自nginx的日志很好。 泊坞窗映像将访问日志符号链接到/ dev / stdout。
我在/ proc /#/ fd / 1中查看了每个正在运行的进程,它们都指向同一管道
l-wx------ 1 root root 64 Dec 15 21:18 1 -> pipe:[28195]
from ls
我不确定如何使它工作。
server.py
看起来像这样吗?
import time
while True:
print('something')
time.sleep(1)
当您在容器内的while循环内打印和休眠时,Python会做一些奇怪的事情。 为了使其工作,您需要手动刷新循环的打印语句:
import time
import sys
while True:
print('something')
sys.stdout.flush()
time.sleep(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.