Stop losing your cron job environment variables

Cron & Task Scheduling (Cron/Systemd Timers/At)

Stop losing your cron job environment variables

🧩 The Challenge

You finally finish a script that works perfectly in your shell, only for it to fall flat on its face the second it runs under cron. It’s usually because your PATH is missing or some shell alias you depend on just isn’t there, and debugging that silent failure is a nightmare.

💡 The Fix

Don’t rely on the crontab environment to guess where your binaries live. Explicitly source your environment or set your path right inside the crontab file to keep things predictable.

PATH=/usr/local/bin:/usr/bin:/bin
* * * * * /usr/local/bin/my-script.sh >> /var/log/myscript.log 2>&1

⚙️ Why It Works

Setting the PATH variable at the top of your crontab forces cron to use the directories you specify instead of its own stripped-down default. It’s the only way to make sure your binaries are found regardless of how limited the cron subshell environment actually is.

🚀 Pro-Tip: Always pipe your stderr to your stdout in the crontab line so you can actually read the error messages in your log file instead of wondering why nothing happened.

Linux Tips & Tricks | © ngelinux.com | 11/23/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted