0
nohup python3 main.py > log.output &

So with this, I am getting some output by my framework but my individual print statements are not being logged to log.output. Is there anyway to fix this?

Output under nohup

nohup: ignoring input * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 571-306-491 XXX - - [28/Jan/2018 17:56:42] "POST /TestEndpoint HTTP/1.1" 201 - XXX - - [28/Jan/2018 17:57:00] "POST /TestEndpoint HTTP/1.1" 201 -

Normal running output

python3 main.py
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 571-306-491
test endpoint
XXX - - [28/Jan/2018 18:01:52] "POST /TestEndpoint HTTP/1.1" 201 -

Tried all of these :

nohup python3 -u main.py &
nohup python3 main.py > log.output 2>&1 &
nohup python3 -u main.py > log.output 2>&1 &
nohup python3 main.py > log.output &
nohup python3 -u main.py > log.output &

only this shows the correct output python3 main.py

  • Possible duplicate of [Nohup is not writing log to output file](https://stackoverflow.com/questions/12919980/nohup-is-not-writing-log-to-output-file) – Equinox Jan 28 '18 at 17:52
  • @venky__ I tried every solution in there, none of them cause my print to my displayed – asdasd2a43qaad Jan 28 '18 at 18:02

1 Answers1

0

print by default sends data to sys.stdout, but its buffered unless you explicitly stated otherwise.

Could try redirecting stderr in stdout also, just to check if nohup affects it in some way.

nohup python3 main.py > log.output 2>&1 &

Ilija
  • 1,556
  • 1
  • 9
  • 12
  • Still not getting print statements, there should be a print statement between every POST, they are not showing up. They show up just fine if I run this in the foreground without using nohup / & .. I edited main post to show log output – asdasd2a43qaad Jan 28 '18 at 17:57
  • maybe there is ```nohup.out``` file that all printed lines get redirected to, and is located in the same folder where you were when you run this command – Ilija Jan 28 '18 at 18:10
  • I tried that as well `nohup python3 -u main.py &` puts the output to nohup.out and that does not contain prints either – asdasd2a43qaad Jan 28 '18 at 18:11
  • 2
    how about calling ```print``` with ```print('...', flush=True)?``` – Ilija Jan 28 '18 at 18:16
  • Thanks works ... but I have tons of prints. Is there not a better solution then adding `flush=True` to every print statement? – asdasd2a43qaad Jan 28 '18 at 18:20
  • ```flush``` flag is added in Python 3.3. Before that you had to call ```sys.stdout.flush```. Not sure if that is convenient too, but you could call if in certain points in code. Something like this https://stackoverflow.com/a/230774/3029287 – Ilija Jan 28 '18 at 18:25