Configuring cron with environment variables

Beyond the cron job declarations themselves, the crontab specification allows you to set environment variables directly within your crontab that are made available to your cron jobs when they run. In modern versions of cron, environment variables can also be set that modify the behavior of Cron itself.

  1. MAILTO and MAILFROM

    Anything your command writes to stdout or stderr is collected by cron and emailed to you after your job completes. Usually this is an unwelcome source of monitoring spam and the most common advice to prevent the email is to ensure your job never writes to stderr or stdout. That technique is the correct approach to stop emails from a single job, but there’s an easier way to prevent cron from ever sending an email, setting a MAILTO environment variable:

    MAILTO=""

    Alternatively, if you find cron emails useful, use MAILTO to specify the receipient email and optionally set a custom "from" address using the MAILFROM variable for easier mail filtering and handling. Note: your host will need a Message Transfer Agent (MTA) that is configured to send external email.

    Compatibility: Works everywhere

  2. SHELL

    By default, cron uses the “dash” shell at /bin/sh to invoke your commands at their scheduled time and some features that work at an interactive bash prompt are unavailable in dash. You can tell cron to use bash, zsh or any other shell using the SHELL variable:

    SHELL=/bin/bash

    Compatibility: Works everywhere

  3. CRON_TZ

    Cron jobs are run in whatever timezone the system clock is set to. By default this is UTC, but cron jobs are commonly used to perform actions based on wall clock time and it’s a common ritual to calculate timezone offsets to ensure your job is properly scheduled. By adding a CRON_TZ declaration in your crontab, you can control this without the hassle of calculating offsets and dealing with daylight savings time changes:

    CRON_TZ=America/New_York

    Compatibility: Not available on some systems and called TZ on others. See man 5 crontab for details.

  4. RANDOM_DELAY

    On systems under load hotspots can be unintentionally created when cron invokes multiple jobs at the same time. Delaying just a single cron job can best be done by adding a sleep to the command, but the easiest way to add a random delay to all of your jobs is to use the RANDOM_DELAY variable to define the maximum number of minutes to delay.

    RANDOM_DELAY=3

    Compatibility: Not available on some systems. See man 5 crontab for details.

  5. There are no cron configuration options for monitoring and alerting. Monitor your cron jobs with Cronitor to easily collect output, capture errors and alert you when something goes wrong.

Our software, CronitorCLI, includes the free tools we always wished we had.  Scan your system and list every cron job in seconds using cronitor list.  Run any cron job instantly using cronitor select.