Stop cron from choking on your environment variables

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

Stop cron from choking on your environment variables

🧩 The Challenge

You spend all afternoon perfecting a script that runs perfectly from your shell, only to watch it fail miserably when it hits cron because your path isn’t what you think it is. It’s infuriating when you realize your cron environment is basically a desert compared to your interactive login session.

💡 The Fix

Stop relying on the default cron environment entirely by defining your shell and PATH directly inside the crontab file. This forces the scheduler to respect the same paths your manual scripts use.

SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
* * * * * /path/to/your/script.sh

⚙️ Why It Works

Adding these variables at the top of the file explicitly overrides the stripped-down, minimal defaults that cron uses. It ensures your script isn’t looking for binaries in a directory that doesn’t exist or isn’t in the standard search path.

🚀 Pro-Tip: Always use full absolute paths for binaries inside the script too; never assume /usr/local/bin is in the PATH if you haven’t set it.

Linux Tips & Tricks | © ngelinux.com | 9/9/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted