简体   繁体   English

使用crontab执行脚本时登录python

[英]Logging in python while executing script with crontab

I use the python's logging module to log what's happening in the script. 我使用python的日志记录模块记录脚本中发生的情况。 It works perfectly if I run the script manually, but if I run it through crontab nothing gets logged. 如果我手动运行脚本,它会完美运行,但如果通过crontab运行脚本,则不会记录任何内容。 So there is probably some setting missing but I'm at loss where to find out which setting to change. 因此,可能缺少一些设置,但是我不知所措,无法找到要更改的设置。

For reference this is the settings I use for the logging. 作为参考,这是我用于日志记录的设置。

{
    "version": 1,
    "disable_existing_loggers": false,
    "formatters": {
        "simple": {
            "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        }
    },

    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "level": "DEBUG",
            "formatter": "simple",
            "stream": "ext://sys.stdout"
        },

        "info_file_handler": {
            "class": "logging.handlers.RotatingFileHandler",
            "level": "INFO",
            "formatter": "simple",
            "filename": "info.log",
            "maxBytes": "10485760",
            "backupCount": "5",
            "encoding": "utf8"
        },

        "error_file_handler": {
            "class": "logging.handlers.RotatingFileHandler",
            "level": "ERROR",
            "formatter": "simple",
            "filename": "errors.log",
            "maxBytes": "10485760",
            "backupCount": "5",
            "encoding": "utf8"
        }
    },

    "loggers": {
        "my_module": {
            "level": "ERROR",
            "handlers": ["console"],
            "propagate": "no"
        }
    },

    "root": {
        "level": "INFO",
        "handlers": ["console", "info_file_handler", "error_file_handler"]
    }
}

Running things under Cron can be a little tricky: 在Cron下运行事情可能会有些棘手:

  1. it starts in the crontab user's $HOME directory. 它从crontab用户的$HOME目录开始。 If there's an errors.log file there, it might not work. 如果那里有errors.log文件,则可能无法正常工作。 Check the permissions. 检查权限。

  2. I've never seen ext://sys.stdout format before. 我以前从未见过ext://sys.stdout格式。 Cron routes stdout to email, which tends not to work. Cron将stdout路由到电子邮件,这通常不起作用。

I suggest routing all messages to /tmp/myscript.log -- it's easier to check. 我建议将所有消息路由到/tmp/myscript.log -更容易检查。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM