Blog · Troubleshooting
My script works when I run it, but not from cron
The script runs fine in your shell and does nothing on schedule. Nine times out of ten it's the environment, not the script. Here are the usual culprits.
First, see what cron actually sees
Cron runs your job with a bare environment and no terminal, so any error just vanishes. Before guessing, capture the output to a log:
0 3 * * * /usr/local/bin/job.sh >> /var/log/job.log 2>&1
Now the next run leaves a real error message instead of silence, and it's usually one of these:
1. PATH is nearly empty
Cron's PATH is minimal, so python, node or pg_dump often aren't found. Use absolute paths (/usr/bin/python3), or set PATH at the top of the crontab:
PATH=/usr/local/bin:/usr/bin:/bin
2. Wrong working directory
Cron starts in the user's home directory, not your project, so relative paths break. cd into the right place first, or make every path absolute:
0 3 * * * cd /srv/app && ./run.sh
3. Missing environment variables
Cron doesn't read ~/.bashrc or ~/.profile, so variables like DATABASE_URL simply aren't set. Load them explicitly in the script, e.g. set -a; . /srv/app/.env; set +a, or define them in the crontab.
4. An unescaped % in the command
Inside a crontab line, % means a newline. A command like date +%Y-%m-%d gets silently cut off at the first %. Escape each one with a backslash:
0 3 * * * /usr/local/bin/backup.sh db-$(date +\%Y-\%m-\%d).sql
5. Wrong user or permissions
crontab -e edits the current user's crontab, so a job that needs root, or a file only you can read, fails when it runs as the cron user. Check whose crontab it's in, and that the script is executable (chmod +x).
Fixed it? You've solved today's problem. Cron still won't tell you if the same job silently stops next month, after a reboot, a bad deploy, or a dropped crontab line.
Know the next time, automatically
That second half is exactly what Mortemain is: a dead man's switch for scheduled jobs. Your job checks in on each run with one HTTP request, and if a check-in ever goes missing, Mortemain alerts you. No agent, no SDK, EU-hosted.
Add a check-in to your cron job →
Try Mortemain free (20 checks, no credit card).