1

I use the python's logging module to log what's happening in the script. It works perfectly if I run the script manually, but if I run it through crontab nothing gets logged. 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"]
    }
}

1 Answer 1

2

Running things under Cron can be a little tricky:

  1. it starts in the crontab user's $HOME directory. If there's an errors.log file there, it might not work. Check the permissions.

  2. I've never seen ext://sys.stdout format before. Cron routes stdout to email, which tends not to work.

I suggest routing all messages to /tmp/myscript.log -- it's easier to check.

Sign up to request clarification or add additional context in comments.

2 Comments

There is no errors.log or info.log there. The permissions of $HOME is drwx------ So that means the current user has permission to do things there. What kind of permission does Cron require? Routing all messages from where? In crontab?
Cron by default routes /dev/stdout to email, but email generally isn't set up correctly, so Cron gets confused. 1) write logs to a file explicitly. Also: 2) do tail -f /var/log/syslog to see what Cron is doing. It's fiddly :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.