Issue
I am using the python logging module and crontab to run the script, at the beginning of every month. Now i noticed that only the debug level gets logged when started with crontab (tested it out by starting the script every minute), but when started manually the logging works just as intended. Can someone explain to me why that is the case?
Solution
One possible reason why you are only seeing debug level logs when running your script with crontab is that the environment variables used by your script are different when running with crontab. The environment variables are a set of dynamic named values that can affect the way running processes behave on an operating system.
When you run your script manually, it runs in the environment of the user who executes it. However, when running with crontab, the script runs in a different environment that might not have the same environment variables set up.
To debug this issue, you can add the following code to your script to log the environment variables:
import os
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Log the environment variables
for key, value in os.environ.items():
logger.debug(f"{key}: {value}")
This will log all the environment variables to the debug level, which might help you understand what is different when running with crontab. You can also try setting the environment variables explicitly in your crontab file to match the environment when running manually.
For example, you can add the following line to your crontab file before the script runs:
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
This sets the PATH environment variable to the same value as when running manually.
Another thing to consider is that crontab might run your script in a different directory than when you run it manually. You can use the os.getcwd() function to log the current working directory and see if it matches when running manually and with crontab.
Hope this helps you solve the issue!
Answered By - Maksym Answer Checked By - David Marino (WPSolving Volunteer)