Crontab not executing a Python script? [duplicate] Crontab not executing a Python script? [duplicate] python python

Crontab not executing a Python script? [duplicate]


There are a lot of half answers across the internet so I thought I would capture this to save someone else some time.

First, cronjob does a poor job of telling you where this is failing. I recommend sending stderr output to a log file like this:

Crontab Command:

# m h  dom mon dow   command* * * * * /path/to/your_file.sh >> out.txt  2>&1

As this is likely running the command as user, check home directory for the log file. Note this script runs every minute which is good for debugging.

The next issue is you probably have a path problem... as script likely is trying to execute from your home directory. This script sets the current directory, echos it to file, and then runs your program.

Try this :

Script File

#!/bin/shcd "$(dirname "$0")";CWD="$(pwd)"echo $CWDpython your_python_file.py

Hope this saves someone else some debugging time!!!


What happens when you type

/home/me/project/myscript.py into the shell?

Can you explicitly use /usr/bin/python in your crontbb command?

Can you either use an absolute path to your test.db or cd to the correct directory then execute your python script?

This is helpful to have debug statements in your python and log some data. Crontab can be very tricky to debug.


It is possible that the script does not start because it cannot locate the python interpreter. Crontab environment may be very different from the shell environment which you are using. The search paths may be differ significantly.Also, you test your script by starting the python interpreter explicitly while you expect the crontab to only start the script.I put this line at the top of my python scripts:

\#!/bin/env python

This line will help locate the interpreter regardless of which directory it is installed in as long as it is in the search path.