Introduction
In Linux, automating recurring tasks is essential for system maintenance. Whether it's running daily backups, cleaning up temporary files, or generating weekly reports, these tasks are handled by a system utility called Cron.
Cron is a strictly time-based job scheduler. It relies on a specific configuration syntax to determine exactly when to run commands. The configuration table is often referred to as a "crontab".
Cron has been part of Unix culture for decades. Long before modern workflow platforms and cloud schedulers, administrators relied on cron to run backups, cleanup jobs, reports, and maintenance tasks at predictable times.
Looking at a crontab entry like 0 2 * * * can be confusing at first. In this lab, you will learn to interpret and write this syntax. You will:
- Explore the System Crontab: View the global cron configuration file.
- Decode the Syntax: Understand the 5 time-and-date fields.
- Draft a Schedule: Construct a valid cron expression for a daily task.
View the System Cron Table
A fast way to understand cron syntax is to look at the system-wide configuration file, /etc/crontab. This file usually contains a helpful header explaining the field layout.
This file is useful as both a real configuration file and a teaching reference. Many Linux systems include comments here because cron syntax is compact, powerful, and easy to misread until you understand the field order.
Run the following command to view it:
cat /etc/crontab
Focus on the Example of job definition section in the output (lines may vary slightly by distribution):
## .---------------- minute (0 - 59)
## | .------------- hour (0 - 23)
## | | .---------- day of month (1 - 31)
## | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
## | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
## | | | | |
## * * * * * user-name command to be executed
This diagram shows that every cron schedule consists of 5 fields followed by the command to run:
- Minute (0-59)
- Hour (0-23, 24-hour format)
- Day of Month (1-31)
- Month (1-12)
- Day of Week (0-7, where both 0 and 7 represent Sunday)
The asterisk * is a wildcard that means "every". For example, * in the Month field means "every month".
Cron expressions are compact because each field narrows the schedule. Together, the fields describe a repeating point in time. Reading them left to right as minute, hour, day, month, weekday is one of the most important habits to build early.
Example: 17 * * * * means "At minute 17, of every hour, every day."
Prepare a Syntax File
Now that we understand the structure, let's practice writing an entry.
We will create a text file to draft our cron expression. This is a good practice before adding it to the actual system scheduler.
That drafting step matters because a cron entry is just plain text, but it has real effects once installed. Writing the expression separately helps you think about the schedule before turning it into automation.
Ensure you are in your project folder:
cd ~/project
Create an empty file named cron_syntax.txt:
touch cron_syntax.txt
Verify it exists:
ls -l cron_syntax.txt
Write a Daily Schedule Syntax
Your task is to construct a cron expression for a task that needs to run "Every day at 2:00 AM".
Let's break down the 5 fields for this requirement:
- Minute: We want exactly 2:00. This is the 0th minute. Value:
0. - Hour: We want 2 AM (24-hour format). Value:
2. - Day of Month: We want it to run every day of the month. Value:
*. - Month: We want it to run every month of the year. Value:
*. - Day of Week: We want it to run regardless of the day of the week. Value:
*.
Combining these, the expression is: 0 2 * * *.
Let's append this syntax to your file:
echo "0 2 * * *" >> cron_syntax.txt
Verify your entry:
cat cron_syntax.txt
This syntax 0 2 * * * tells the system: "When the minute is 0 AND the hour is 2, run the command."
This is the mental model you should keep: cron is matching the current time against all five fields. When the current time fits the pattern, cron launches the job.
Summary
In this lab, you learned the fundamentals of cron scheduling:
- System Configuration: You explored
/etc/crontabto see the standard layout. - Field Structure: You identified the 5 fields: Minute, Hour, Day of Month, Month, and Day of Week.
- Writing Expressions: You successfully created a syntax for a daily job (
0 2 * * *).
Understanding this syntax is crucial for automating any routine task in a Linux environment.



