Issue
From this python script:
import logging
print('normal print')
logging.error('logger test error')
If I run this script from command line I get:
$ python3 ./test.py
normal print
ERROR:root:logger test error
Now if I set up a cron job with the same script as follow:
25 10 * * * python3 ~/test.py
I get the following email:
...
Content-Type: text/plain; charset=UTF-8
...
Date: Fri, 8 Oct 2021 10:25:01 +0200 (CEST)
ERROR:root:logger test error
normal print
We see that the output is reversed. What's happening here ?
Solution
I found an eligible answer at https://unix.stackexchange.com/questions/182537/write-python-stdout-to-file-immediately
Basically, I suppose that cron redirects the command output somewhere to generate the email and that buffering is happening - which is not the case for console output. Here, print()'s output is buffered (eg. not flushed). However, logging
does flush the stream after each call.
Answered By - martin-h