Cron

What is Cron?

Cron is a time-based job scheduler in Unix-like operating systems, including Linux and macOS. It allows users to run scripts, commands, or software programs at specified intervals, such as every minute, hourly, daily, weekly, or monthly. Cron is commonly used for automating repetitive tasks, performing system maintenance, and running background jobs that do not require user interaction.

How does Cron work?

Cron uses a configuration file called the “crontab” (short for “cron table”) to schedule tasks. Each user on the system can have their own crontab file, which lists the commands or scripts to be executed along with their corresponding schedule. The cron daemon, a background process that runs continuously, checks the crontab files for scheduled tasks and executes them at the specified times.

Cron syntax:

Cron uses a specific syntax to define the schedule for each task. Each line in the crontab file represents a separate job and consists of six fields:

* * * * * /path/to/command arg1 arg2
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday is both 0 and 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Each field can contain a specific value, a range (e.g., 1-5), a list of values (e.g., 1,3,5), or an asterisk (*) to represent “any” value.

Cron example:

To schedule a script to run every day at 3:30 PM, you would add the following line to your crontab file:

30 15 * * * /path/to/your/script.sh

To edit your crontab file, you can use the following command:

$ crontab -e

Cron resources: