I'm executing a python script every 30 minutes with cron, but it stops at the import statement and I don't understand why because I don't get any feedback.
In my crontab I have */30 * * * * sh exec_script.sh
The file exec_script.sh is
cd /home/ziofil/python_scripts
python script_30_mins.py
In the python script I have
import logging
logging.basicConfig(filename="explicit_log.txt",level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.debug("script is executed")
# in the python_scripts folder there is a subfolder utilities/ that
# contains utility_1.py, were I define the class Utility
from utilities.utility_1 import Utility
logger.debug("Utility loaded")
utility = Utility()
logger.debug("object creation")
utility.do_your_thing()
logger.debug("done its thing")
If I execute python script_30_minutes.py from the terminal, everything works just fine, but with cron the execution stops at the import statement (I read "script is executed", but noting else).
What is going wrong?
UPDATE: I put the import statement in a try/except block and it logged the exception: "No module named utilities.utility_1". I also tried to set the PYTHONPATH variable in the shell script to /home/ziofil/python_scripts, but it still doesn't work.
I found out what the problem was. @UweMannl was right all along. After @mhawke pointed out that cron's environment is different than the one of my terminal, I thought that perhaps also the python binary could be different and indeed it was: I want /home/ziofil/anaconda3/bin/python and cron was using /usr/bin/python.
I modified the last line of the script to /home/ziofil/anaconda3/bin/python script_30_mins.py and everything works!
cron runs processes with a different environment to that of your terminal. Possibly you have set PYTHONPATH in your terminal, but not in your cron environment. If that is the case you can add it to your shell script:
export PYTHONPATH=/home/ziofil/python_scripts/whatever:$PYTHONPATH
cd /home/ziofil/python_scripts
python script_30_mins.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With