Cron job environment variables

By: Shane Harter|Last Updated: Mar 17, 2023

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.


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:


Alternatively, if you find cron emails useful, use MAILTO to specify the recipient 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


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:


Compatibility: Works everywhere


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:


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


Some implementations of cron, including anacron, allow random delay to all of your jobs is to use the RANDOM_DELAY variable to define the maximum number of minutes to delay.


Tip: If you want to add a delay to a single job, you can use the sleep command inline

    0 0 * * Mon-Fri sleep ${RANDOM:0:1} && /path/to/executable

Are your cron jobs running?

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.