Often, crontab
scripts are not executed on schedule or as expected. There are numerous reasons for that:
- wrong crontab notation
- permissions problem
- environment variables
This community wiki aims to aggregate the top reasons for crontab
scripts not being executed as expected. Write each reason in a separate answer.
Please include one reason per answer – details about why it's not executed – and fix(es) for that one reason.
Please write only cron-specific issues, e.g. commands that execute as expected from the shell but execute erroneously by cron.
Best Answer
Different environment
Cron passes a minimal set of environment variables to your jobs. To see the difference, add a dummy job like this:
Wait for
/tmp/env.output
to be created, then remove the job again. Now compare the contents of/tmp/env.output
with the output ofenv
run in your regular terminal.A common "gotcha" here is the
PATH
environment variable being different. Maybe your cron script uses the commandsomecommand
found in/opt/someApp/bin
, which you've added toPATH
in/etc/environment
? cron ignoresPATH
from that file, so runnningsomecommand
from your script will fail when run with cron, but work when run in a terminal. It's worth noting that variables from/etc/environment
will be passed on to cron jobs, just not the variables cron specifically sets itself, such asPATH
.To get around that, just set your own
PATH
variable at the top of the script. E.g.Some prefer to just use absolute paths to all the commands instead. I recommend against that. Consider what happens if you want to run your script on a different system, and on that system, the command is in
/opt/someAppv2.2/bin
instead. You'd have to go through the whole script replacing/opt/someApp/bin
with/opt/someAppv2.2/bin
instead of just doing a small edit on the first line of the script.You can also set the PATH variable in the crontab file, which will apply to all cron jobs. E.g.