Python http server 与 nohup 使用时无法打印日志的问题
nohup python -u -m http.server 2>&1 &
By default, Python’s stdout and stderr are buffered. As other responders have noted, if your log files are empty then (assuming your logging is correct) the output has not been flushed.
The link to the script is no longer valid, but you can try running your script either as python3 -u filename.py or the equivalent PYTHONUNBUFFERED=x python3 filename.py. This causes the stdout and stderr streams to be unbuffered.
A full example that uses the standard library’s http.server module to serve files from the current directory:
PYTHONUNBUFFERED=x python3 -m http.server &> http.server.log & echo $! > http.server.pid
All output (stdout & stderr) is redirected to http.server.log, which can be tailed, and the process ID of the server is written to http.server.pid so that you can kill the process by kill $(cat http.server.pid).
Leave a Reply