No error messages with nohup and python?

I am running a python script on a linux server without nohup:

nohup python3 ./myscript.py > ./mylog.log & 

It works and the script writes the log file, but the problem is that the python error / exception messages do not seem to be written to the log file. How could I achieve this?

Is this something related to stderr? (but he says: "nohup: redirecting stderr to stdout" when I run the script.)

This is a long script, and after a while the script stops working due to some kind of problem and with the missing error messages I don’t know why. Problems always arise after a few days, so this is really a pain for debugging.

edit: Could this have something to do with a flash? Since my own prints use flash, but maybe python errors are not so that they do not appear in the file after the script is interrupted?

+4
source share
3 answers

I have found a reason. It really was a buffering problem (see My edit above). :)

 nohup python3 -u ./myscript.py > ./mylog.log & 

With the python -u option, it works. This disables buffering.

Now I can go looking for mistakes ...

+9
source

You are only redirecting stdout. Error messages are sent to stderr. Restart your script as follows:

 nohup python3 ./myscript.py &> ./mylog.log & 

&> redirects all output (stdout and stderr) to the log file.

+2
source

with nohup, the error will not be logged unless you redirect the error logs to the second file, as shown below

 nohup python myscript.py > myscript.out 2> myscript.err 
0
source

Source: https://habr.com/ru/post/1486763/


All Articles