3

I use Python3.6 to write a simple HTTP server to redirect all requests.

The file I written can be found here

I can see output in both Win8.1 CMD & Ubuntu 16.04.3 Bash. However , whatever I try any of those methods below , it doesn't work , the log cannot be saved into the file.

nohup python3 ./filename.py > ./logfile 2>&1 &
python3 ./filename.py > ./logfile 2>&1 &
setsid ./filename.py > ./logfile 2>&1 &

I tried to use:

import sys
logfile = open('logfile.log','w')
sys.stdout = logfile
sys.stdin = logfile
sys.stderr = logfile

It didn't work.

2
  • 1
    What do you mean by "didn't work"? If you're trying to read the file while the server is running then the output probably hasn't been flushed yet Commented Oct 10, 2017 at 11:46
  • Nope. I mean that I tried to run it and after several hours , the logfile is still empty. I have to see the log like this > 2017/1/1 GET / - 301 . However , I see nothing in the log file. Commented Oct 11, 2017 at 1:12

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

Comments

1

i've tried your code on Ubuntu 16.04 and it worked like charm.

import sys
logfile = open('logfile.log','w')
sys.stdout = logfile
sys.stdin = logfile
sys.stderr = logfile

3 Comments

See the screenshot here.Maybe there's some shortage of dependencies ? i.loli.net/2017/10/11/59dd7aa56f3ca.jpg
@PotatoChips have you read the manual page for nohup? nohup redirects away from the terminal, so trying direct the remaining output (of which where is none) into a log file won't give you anything in that log file. And in the other case your server is still running so it might not have been flushed. So run it without nohup and shut it down, then check the output. If you need to read the log while the server is running, make sure the output is being flushed.
I tried to run without nohup , and after serveral hours I killed the process.The log file still remains empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.