Cron & Task Scheduling (Cron/Systemd Timers/At)
Stop guessing why your cron job failed
đź§© The Challenge
Ever stared at a cron job entry, knowing it should run, but it just sits there silently doing nothing? Your script runs fine from the command line, but cron spits out an obscure error or, worse, no output at all. Nobody tells you this, and it’s wasted hours for many of us.
đź’ˇ The Fix
There’s a simple trick to mimic cron’s bare-bones environment right from your terminal. This lets you run your command exactly as cron would see it, making any missing binaries or environment variable issues instantly obvious.
env -i PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" HOME="/home/youruser" USER="youruser" SHELL="/bin/bash" /bin/bash -c "/path/to/your/script.sh"
⚙️ Why It Works
Cron runs jobs in an incredibly sparse environment, far different from your interactive shell. It doesn’t inherit your usual `PATH`, aliases, or custom environment variables. Running with `env -i` strips away your current environment, forcing you to explicitly define key variables like `PATH`, `HOME`, `USER`, and `SHELL` that cron provides, quickly exposing what your script is missing.
🚀 Pro-Tip: Need to test a job from another user’s crontab? Just prepend `sudo -u that_user` before the `env -i` command.
Linux Tips & Tricks | © ngelinux.com | 9/17/2026
