Setting Up Default Cron Path

I’ve run into a ton of issues working with crons, mostly with the $PATH variable screwing things up. Scripts work when run manually on the command line, but fail when run in cron. Very annoying.

I’ve asked a bunch of Linux sys admins how to fix this - and the answer is always “put the full path in your scripts” which to me in unacceptable as it introduces the possibility of human error. Fixing the underlying problem is always preferred.

You can add a PATH variable to the top of your crontab that all scripts will use instead of the default one which doesn’t access /usr/local/bin or any other PATH variables you had set elsewhere.

At the top of your crontab, simply put something like this:

PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:.

This has been an issue for me for years. It also solves the issue of php shell_exec, exec, system, and passthru not having access to the PATH variable.

You’ll need to keep your PATH up to date, but it’s easier than manually editing every script, or messing around with hacks to get every script to work properly.

Enjoy!

If you found this post helpful, please consider sharing to your network. I'm also available to help you be successful with your distributed systems! Please reach out if you're interested in working with me, and I'll be happy to schedule a free one-hour consultation.