python 来自 crontab 与控制台的脚本:打印和日志记录的顺序不同
python script from crontab vs console : print and logging ordered differently
来自这个 python 脚本:
import logging
print('normal print')
logging.error('logger test error')
如果我从命令行 运行 这个脚本,我得到:
$ python3 ./test.py
normal print
ERROR:root:logger test error
现在,如果我使用如下相同的脚本设置一个 cron 作业:
25 10 * * * python3 ~/test.py
我收到以下电子邮件:
...
Content-Type: text/plain; charset=UTF-8
...
Date: Fri, 8 Oct 2021 10:25:01 +0200 (CEST)
ERROR:root:logger test error
normal print
我们看到输出是相反的。
这里发生了什么?
我在 https://unix.stackexchange.com/questions/182537/write-python-stdout-to-file-immediately
找到了符合条件的答案
基本上,我想 cron 将命令输出重定向到某处以生成电子邮件并且正在发生缓冲 - 控制台输出不是这种情况。在这里,print() 的输出被缓冲(例如,未刷新)。但是,logging
确实 在每次调用后刷新流。
来自这个 python 脚本:
import logging
print('normal print')
logging.error('logger test error')
如果我从命令行 运行 这个脚本,我得到:
$ python3 ./test.py
normal print
ERROR:root:logger test error
现在,如果我使用如下相同的脚本设置一个 cron 作业:
25 10 * * * python3 ~/test.py
我收到以下电子邮件:
...
Content-Type: text/plain; charset=UTF-8
...
Date: Fri, 8 Oct 2021 10:25:01 +0200 (CEST)
ERROR:root:logger test error
normal print
我们看到输出是相反的。 这里发生了什么?
我在 https://unix.stackexchange.com/questions/182537/write-python-stdout-to-file-immediately
找到了符合条件的答案基本上,我想 cron 将命令输出重定向到某处以生成电子邮件并且正在发生缓冲 - 控制台输出不是这种情况。在这里,print() 的输出被缓冲(例如,未刷新)。但是,logging
确实 在每次调用后刷新流。