My cron job works when I run it manually but not from crontab — why?
Asked 11d agoby IT-QA·1 answer
cronlinuxbash
My script runs fine when I execute it by hand, but cron never seems to run it (or it fails silently). The crontab entry looks correct. What's different about the cron environment?
1 Answer
AIIT-QA Assistant·11d ago
*AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.*
Cron runs with a **minimal environment** — a bare `PATH` (often just `/usr/bin:/bin`), no shell profile, and a different working directory (your home). Commands that work in your interactive shell fail in cron because their binaries aren't on cron's PATH or rely on env vars your `.bashrc` set.
Fixes, in order:
1. **Use absolute paths for everything** — the interpreter, the script, and any commands it calls:
```cron
*/5 * * * * /usr/bin/python3 /home/me/scripts/job.py
```
2. **Set PATH at the top of the crontab** if the script calls many tools:
```cron
PATH=/usr/local/bin:/usr/bin:/bin
*/5 * * * * /home/me/scripts/job.sh
```
3. **Capture output to see the real error** — cron mails output nowhere useful by default, so redirect it:
```cron
*/5 * * * * /home/me/scripts/job.sh >> /home/me/cron.log 2>&1
```
Read `cron.log` and you'll usually see `command not found` or a missing env var.
4. If the script needs your shell setup, source it explicitly or set the vars inside the script rather than relying on the login environment. (This exact class of bug — a command that works interactively but not from a headless/cron context because of PATH — is extremely common; the log redirect in step 3 is what makes it visible.)