简体   繁体   中英

No output from process running in background from shell script

I wrote a small script to test some servers in a docker container.

#!/bin/bash
echo started
python /opt/server.py &
nginx -g 'daemon off;'

The python server writes some information that we need to see but it doesnt show up when I run docker run -P image or if I run docker run -dP image and check with docker logs.

The python server uses print and sys.stdout.write . Both dont show up I tried redirecting the output python /opt/server.py >&1 and /dev/stdout. Neither worked. Logs from nginx are fine. The docker image symlinks the access logs to /dev/stdout.

I looked in /proc/#/fd/1 for each proccess running and theyre all pointing to the same pipe

l-wx------ 1 root root 64 Dec 15 21:18 1 -> pipe:[28195] from ls

Im not sure how to get this working.

Does server.py look something like this?

import time
while True:
   print('something')
   time.sleep(1)

Python does something weird when you print and sleep inside a while loop inside containers. To make it work, you need to manually flush the looped print statements:

import time
import sys
while True:
   print('something')
   sys.stdout.flush()
   time.sleep(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM