Skip to content

Master Your Scheduled Tasks

Unlock the potential of crontab for streamlined management of scheduled tasks. Explore various functionalities such as testing cronie configurations, editing scheduled tasks, deleting current tasks, and controlling environment variables for your cron jobs.


Test cronie from line 5

> cat /etc/cronie.conf
echo "Hello World - Example 1"
echo "Hello World - Example 2"
echo "Hello World - Example 3"
echo "Hello World - Example 4"
echo "Hello World - Example 5 - What's up?"
echo "Hello World - Example 6"
eval "$(crontab -l | sed -n '5p' | tr -s ' ' | cut -d' ' -f 6-)"

Edit crontab scheduled tasks

crontab -l

Delete all the current users crontab tasks

crontab -r

Print Current Cronies

crontab -l

Environment

When configuring environment variables for your cron jobs, you have multiple options based on your specific needs. These options include:

Setting Environment Variables for Cron Jobs

  1. Declare Variables Directly in Crontab

In this approach, you define variables directly within your crontab file. Below is an example where we configure the PATH variable and a custom application variable. The cron job will search for example.sh in the specified PATH and use the defined environment variables when executing the command. You can add as many variables as needed in your crontab file.

APP_ENV=production
PATH=/root/it/scripts
0 0 * * * example.sh

One advantage of this method is that the variables are easily identifiable within the crontab file.

Note: Unlike bash scripts, the "export" keyword is neither required nor allowed in this context.

  1. Declare Variables as Part of the Command

Environment variables can be declared inline when running a command in Linux and other POSIX operating systems, and this approach also works with crontab. Here's an example where two jobs receive different values for the same variable:

0 * * * * APP_ENV=production ~/script1.sh  # Every hour
0 0 * * * APP_ENV=staging ~/script2.sh     # Once a day

This is the preferred method if you require different values for variables in different jobs.

  1. Load Variables from a File

If you have numerous environment variables or they are centrally managed by tools like Ansible or Consul, you can store them in a file for reuse. You can then source that file as part of your command string, and any exported variables will become part of the job's environment:

0 * * * * source ~/script1.conf; ~/script1.sh

Controlling Cron Behavior with Environment Variables

Environment variables declared directly in a crontab file are also accessible to cron itself and can be used to control its behavior. Here are five such variables:

  1. PATH

Specify locations where cron should search for scripts and binaries. This is essential when a command lacks a fully-qualified path.

PATH="/users/dataproc/bin:/var/data-analytics/bin:/root/it/scripts"

You can add multiple locations, separated by colons.

  1. MAILTO and MAILFROM

Control email notifications sent by cron. Set MAILTO to an empty string to prevent emails, or specify recipients using MAILTO and set a custom "from" address with MAILFROM.

MAILTO=""
  1. SHELL

Choose a shell other than the default /bin/sh for executing commands in cron. For example, you can use /bin/bash or another shell of your choice.

SHELL=/bin/bash
  1. CRON_TZ

Set the timezone for cron jobs. By default, cron uses UTC, but you can specify a different timezone using CRON_TZ.

CRON_TZ=America/New_York
  1. RANDOM_DELAY

Introduce a random delay in your jobs using the RANDOM_DELAY variable, which defines the maximum number of minutes to delay.

RANDOM_DELAY=3

To add a delay to a single job, you can use the sleep command inline.

Example: Template config for doing your life easier when working with cronies

##########################################################################
###
#### - wuseman help
#### -------------------------------------------------------------------
#### * * * * * command to be executed
#### - - - - -
#### | | | | |
#### | | | | ----- Day of the week (0 - 7) (Sunday=0 or 7)
#### | | | ------- Month (1 - 12)
#### | | --------- Day of the month (1 - 31)
#### | ----------- Hour (0 - 23)
#### ------------- Minute (0 - 59)
###
##########################################################################

##########################################################################
###
####  Special strings and their meanings:
#### --------------------------------------------------------------------
####  @reboot    Run once, at startup
####  @yearly    Run once a year, “0 0 1 1 *”.
####  @annually  (same as @yearly)
####  @monthly   Run once a month, “0 0 1 * *”.
####  @weekly    Run once a week, “0 0 * * 0”.
####  @daily     Run once a day, “0 0 * * *”.
####  @midnight  (same as @daily)
####  @hourly    Run once an hour, “0 * * * *”.
###
##########################################################################

##########################################################################
###
#### - Disable email output for everyone at 3:00 AM daily
###
0 3 * * * /root/backup.sh >/dev/null 2>&1
###
##########################################################################

##########################################################################
###
####  Mail output to a particular email account (e.g., wuseman@nr1.nu)
####
####  To mail output to a specific email address, set the MAILTO variable as follows:
#### --------------------------------------------------------------------
####  MAILTO="username@email.com"
####  0 3 * * * ~/script2.sh >/dev/null 2>&1
###
##########################################################################
MAILTO="name@some_domain.com"

##########################################################################
###
#### - Custom Environment Variables
###
#### Define custom environment variables here, if needed.
###
#### Example: Declare paths or other variables
#### --------------------------------------------------
####  MY_PATH=/custom/path
####  MY_VARIABLE=my_value
###
##########################################################################

##########################################################################
###
####  Additional Environment Variables
###
####  - PATH
####
####  Specify locations where cron should search for scripts and binaries. 
#### This is essential when a command lacks a fully-qualified path.
#### You can add multiple locations, separated by colons.
####
##########################################################################
PATH="$PATH"

##########################################################################
####
####  - MAILTO and MAILFROM
#### 
#### Control email notifications sent by cron. Set `MAILTO` to an 
#### empty string to prevent emails, or specify recipients using `MAILTO` 
#### and set a custom "from" address with `MAILFROM`.
####
##########################################################################
MAILTO=""

##########################################################################
####
####  - SHELL
####
####  Choose a shell other than the default `/bin/sh` for executing
####  commands in cron. For example, you can use `/bin/bash` 
####  or another shell of your choice.
####
##########################################################################
SHELL=/bin/bash

##########################################################################
####
####  - CRON_TZ
####  
#### Set the timezone for cron jobs. By default, cron uses UTC, 
#### but you can specify a different timezone using `CRON_TZ`.
####
##########################################################################
CRON_TZ=America/New_York

##########################################################################
####
####  - RANDOM_DELAY
####     
#### Introduce a random delay in your jobs using the `RANDOM_DELAY` variable, 
#### which defines the maximum number of minutes to delay.
####
####  To add a delay to a single job, you can use the `sleep` command inline.
####
##########################################################################
RANDOM_DELAY=3

##########################################################################
####
####  - Add your cronie jobs below here from the above * * * * format
####
##########################################################################