2023-03-31

What is Cron

What is Cron

Cron is a versatile scheduling utility available in Unix-based systems, such as Linux and macOS. It allows users to automate tasks by running commands or scripts at specified intervals, making it an essential tool for system administrators, developers, and even everyday users.

At its core, Cron is designed to automate repetitive tasks, such as system maintenance, data backups, and report generation. It is particularly useful for managing resource-intensive operations, as these can be scheduled to run during periods of low system usage. By mastering Cron, users can optimize their workflow and improve the overall efficiency of their systems.

Cron Syntax and Components

A Cron job comprises two main elements: a Cron expression and a command.

Cron Expressions

A Cron expression is a series of fields that define the schedule for a task. The expression consists of five fields, each representing a specific unit of time:

  • Minutes (0-59)
  • Hours (0-23)
  • Days of the month (1-31)
  • Months (1-12)
  • Days of the week (0-7; both 0 and 7 represent Sunday)

Each field is separated by a space, and you can use various symbols to define the schedule:

  • Asterisk (*): Represents all possible values for a field (e.g., every minute or every day)
  • Comma (,): Specifies multiple values for a field (e.g., "1,15" means the 1st and 15th day of the month)
  • Hyphen (-): Indicates a range of values for a field (e.g., "1-5" represents Monday through Friday)
  • Slash (/): Defines a step value, often used with an asterisk to represent intervals (e.g., "*/2" in the hours field means every 2 hours)

Examples of Cron Expressions

To help you better understand how to create your own Cron expressions, here are some practical examples with explanations:

  • Run a command every minute:
* * * * * /path/to/command
  • Run a script every 5 minutes:
*/5 * * * * /path/to/script.sh
  • Run a task every day at 3:30 AM:
30 3 * * * /path/to/task
  • Run a job every Monday at 6:00 PM:
0 18 * * 1 /path/to/job
  • Run a backup script on the 1st and 15th day of every month at midnight:
0 0 1,15 * * /path/to/backup.sh
  • Run a task every hour from 9 AM to 5 PM on weekdays:
0 9-17 * * 1-5 /path/to/task
  • Run a report script every 30 minutes during business hours (9 AM to 6 PM), Monday through Friday:
*/30 9-18 * * 1-5 /path/to/report.sh
  • Run a cleanup script at 11:45 PM every last day of the month:
45 23 * * * [ "$(date +'\%m')" != "$(date +'\%m' -d tomorrow)" ] && /path/to/cleanup.sh
  • Run a task every 10 minutes during the first week of the month:
*/10 * 1-7 * 1-5 /path/to/task

Cron Commands

The command is the task to be executed by Cron. It can be a simple shell command, a script, or even a series of commands separated by semicolons.

Setting Up and Managing Crontab Files

Crontab, short for "Cron table," is a file that contains the list of Cron jobs for a specific user. Each user can have their crontab file, allowing for personalized task scheduling.

Creating a Crontab

To create or edit your crontab file, use the following command:

bash
$ crontab -e

This will open the crontab file in the default text editor. If you want to use a different editor, set the VISUAL or EDITOR environment variables accordingly.

Editing a Crontab

Inside the crontab file, add your Cron jobs as separate lines, each with a Cron expression followed by the command. For example:

30 2 * * 1 /home/user/backup.sh

This job will run the backup.sh script at 2:30 AM every Monday. Save and exit the editor to install the crontab.

Listing and Removing Crontabs

To list the contents of your crontab, use the following command:

bash
$ crontab -l

To remove your crontab entirely, use the -r option:

bash
$ crontab -r

References

https://www.hivelocity.net/kb/what-is-cron-job/
https://www.hostinger.com/tutorials/cron-job
https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!