Neurohazard
暮雲煙月,皓首窮經;森羅萬象,如是我聞。

Python http server 与 nohup 使用时无法打印日志的问题

wpadmin~January 6, 2020 /System Management

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).

参考资料

https://stackoverflow.com/a/52236578

Leave a Reply

Your email address will not be published. Required fields are marked *