crontab
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
In this approach, you define variables directly within 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.
Environment variables can be declared inline when running a command in Linux and other POSIX operating systems, and this approach also works with crontab
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.
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 reuser
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:
Specify locations where cron should search for scripts and binaries
PATH="/users/dataproc/bin:/var/data-analytics/bin:/root/it/scripts"
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=""
Choose a shell other than the default /bin/sh for executing commands in cron
bash SHELL=/bin/bash
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
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
sleepcommand inline.
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@info-shell.se)
####
#### 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
####
##########################################################################