What is a CRON Job? An Introduction to Automated Tasks
Share:
In the world of software development and system administration, automation is key. Many tasks, from backing up a database to sending out a daily newsletter, need to happen on a regular schedule without manual intervention. This is where CRON comes in. CRON is a time-based job scheduler in Unix-like operating systems, and it's the invisible workhorse behind countless automated processes on the web.
What is a CRON Job?
A "CRON job" is a single scheduled task. The CRON daemon is a long-running process that reads a configuration file called a "crontab" (cron table). This table contains a list of jobs, with each job consisting of two parts:
- The Schedule: A highly specific time expression that tells CRON *when* to run the task.
- The Command: The actual command or script to be executed.
Decoding the CRON Expression
The most confusing part of CRON for beginners is the schedule expression. It consists of five fields, separated by spaces, that represent a specific time. Understanding these fields is the key to mastering CRON.
┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday; 7 is also Sunday on some systems)
│ │ │ │ │
* * * * * command-to-be-executed
Special Characters
To make scheduling flexible, CRON uses several special characters:
*(Asterisk): The "wildcard." An asterisk in a field means "every." For example, an asterisk in the hour field means the job runs every hour.,(Comma): Specifies a list of values. For example,1,15,30in the minute field means the job runs at 1, 15, and 30 minutes past the hour.-(Hyphen): Specifies a range of values. For example,9-17in the hour field means the job runs every hour from 9 AM to 5 PM./(Slash): Specifies a step value.*/15in the minute field means "every 15 minutes."0-30/10would mean "every 10 minutes during the first half of the hour."
Practical Examples
Let's see how this works in practice.
- Run a script every minute:
* * * * * /path/to/script.sh - Run a backup at 2:30 AM every day:
30 2 * * * /path/to/backup.sh - Send a report at 9:00 AM every Monday:
0 9 * * 1 /path/to/send-report.py - Check for updates every 6 hours:
0 */6 * * * /path/to/check-updates - Run a cleanup task on the 1st of every month at midnight:
0 0 1 * * /path/to/cleanup.sh
Verifying Your Schedule
CRON syntax is powerful but unforgiving. A small mistake can lead to your job running at the wrong time or not at all. It's always a good idea to verify your expression before putting it into production. Our CRON Job Explainer tool is designed for exactly this purpose. You can paste in your CRON expression, and it will translate it into plain, human-readable English, so you can be confident that your automated task will run exactly when you expect it to.
Share: