简体   繁体   English

计划的 cron Python 脚本日志记录的工作方式与手动运行时不同

[英]Scheduled cron Python script logging working differently than it does when run manually

I have a Python script that is running as a cron job every day.我有一个 Python 脚本,它每天都作为 cron 作业运行。 I'm trying to get it to only output when necessary so that cron doesn't email me unless there is a warning or error.我试图在必要时仅将其设置为 output 以便 cron 不会 email 我,除非有警告或错误。 However, DEBUG-level output is still being emailed to me, even though I've set the logging level to WARNING.然而,调试级别的 output 仍在通过电子邮件发送给我,即使我已将日志记录级别设置为警告。

import logging
logging.basicConfig(level=logging.WARNING)

When I run the script through IDLE this works as intended with no output.当我通过 IDLE 运行脚本时,它按预期工作,没有 output。 (If I omit those two lines, I get a bunch of DEBUG output). (如果我省略这两行,我会得到一堆 DEBUG 输出)。 But when the script runs through the cron schedule, I'm being emailed those DEBUG lines.但是当脚本通过 cron 计划运行时,我会收到那些 DEBUG 行的电子邮件。

My crontab is simple:我的 crontab 很简单:

DISPLAY=:0.0
0 6 * * * python3 /home/pi/script.py

Any ideas?有任何想法吗?

I figured it out.我想到了。 I was setting the default logging level after importing the module that was automatically generating that debug output.在导入自动生成调试 output 的模块,我设置了默认日志记录级别。 Kind of obvious now.现在有点明显了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 脚本没有在 cron 下运行,尽管在手动运行时可以工作 - Python script is not running under cron, despite working when run manually 为什么我的 python 脚本在 temal 和 cron 之间的工作方式不同? - Why does my python script work differently between the teminal and cron? python 脚本不能从 cron 运行 - python script does not run from cron 您能否使 python 脚本在导入时和直接运行时表现不同? - Can you make a python script behave differently when imported than when run directly? 为什么 python 脚本在 pycharm 中运行和在命令提示符下运行时的行为不同? - Why does a python script behaves differently when it is run in pycharm and when it is run from a command prompt? 运行Python脚本-Cron - Run Python Script - Cron 为什么我的python类方法本身的行为与我运行模块时的行为不同? - Why does my python class method behave differently by itself than when I run the module? 当将简短的Python脚本作为函数隔离时,为什么运行方式不同? - Why does short Python script run differently when it's isolated as a function? 为什么Windows 10 Task Scheduler执行我的Python脚本时会产生不同的运行 - Why does my Python script run differently when executed by Windows 10 Task Scheduler 当使用 cron 管理器时,脚本在从 cron 作业和使用 django manage.py 的命令行运行时表现不同 - Script behaves differently when run from cron job and from command line using django manage.py when using a cron supervisor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM