How To REALLY Use Cron To Run Scheduled Jobs

It’s nice to think that one can simply use cron to schedule jobs. But in my experience, it’s never simple. For one thing, I’ve never met a cron daemon which doesn’t insist of using sh to run jobs. I don’t run the Bourne Shell — the OG made back in the 1970’s — as my primary shell, so my dot files and path settings are never loaded, resulting in errors that are frustrating to debug, usually when trying to run a shell script.

For another, cron insists of sending the output of jobs as an email to the root owner. This implies that the server has a working sendmail install, and that root is always logging in to administer the system. Again, might be true back in the 70’s, but hardly something you can count on in this day and age.

So, I’m noting a few things that help me use cron effectively with some certainty that each job will run. I know this is not the only way to bend cron to your will, but this list works for me:

  • Run all jobs using bash -l -c. This forces the use of Bash to run the job. Ensure that -l is set so that your dot profile files are loaded also.
  • Explicitly CD to the directory containing your script. Not always necessary, but makes it easier to know exactly where you are when this job runs.
  • Write all output to a log file. This prevents output from being picked up by cron and sent as an email.

Example cron file (this is using MacOS):

# Run repo backup at 10:23 every day
23 10 * * * bash -l -c 'cd /Users/leonmika/src/services/repo-backup && ./backup.sh >/tmp/output.log 2>&1'

Shell