Understanding Linux Cron Job Time Expressions
Creating scheduled tasks in Linux requires an understanding of time expressions, which may be confusing for beginners. For example, the Linux cron job expression: <span>*/5 * * * *</span>
This expression means: Execute the task every 5 minutes.
Expression Breakdown
A cron expression consists of five fields of asterisks (or numbers), each representing different time units. The format is as follows:
* * * * *
| | | | |
| | | | +----- Day of the week (0 - 7) (0 and 7 both represent Sunday)
| | | +------- Month (1 - 12)
| | +--------- Day of the month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)
Now let’s analyze <span>*/5 * * * *</span>:
- 1. First field (minutes):
<span>*/5</span>
- •
<span>*</span>represents “every”. - •
<span>/5</span>represents “step” or “interval”, meaning “every 5 units”. - • Therefore,
<span>*/5</span>together means “starting from 0, every 5 minutes”. - • It will trigger at minute values of
<span>0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55</span>.
<span>*</span>- • Represents “every hour”, meaning all hours of the day meet the condition.
<span>*</span>- • Represents “every day of the month”, meaning every day in the month meets the condition.
<span>*</span>- • Represents “every month of the year”, meaning every month meets the condition.
<span>*</span>- • Represents “every day of the week”, meaning Monday to Sunday meet the condition.
Execution Time Examples
Based on this expression, the task will be executed at the following times (and all similar times):
- • 00:05, 00:10, 00:15, … (every day at midnight)
- • 08:00, 08:05, 08:10, … (8 AM)
- • 12:30, 12:35, 12:40, … (noon)
- • 23:55, 00:00 (next day), 00:05, … (evening to midnight)
In simple terms, your clock will run this task every 5 minutes.
Differences from Other Notations
- •
<span>*/5 * * * *</span>: Every 5 minutes (triggered at 0, 5, 10, …). - •
<span>5 * * * *</span>: Executes at the 5th minute of every hour (e.g., 01:05, 02:05, 03:05). - •
<span>0,5,10 * * * *</span>: Executes at the 0th, 5th, and 10th minute of every hour. This example has the same effect as<span>*/5</span>, but if you need irregular intervals (like<span>0,10,25 * * * *</span>), you must enumerate them with commas instead of using step syntax.
How to Use This Expression
You can edit scheduled tasks using the <span>crontab</span> command.
- 1. Edit the current user’s crontab file:
crontab -e - 2. In the opened file, add a line in the format:
<span><cron expression> <command to execute></span>For example, if you want the system to log the current time every 5 minutes:*/5 * * * * /bin/echo "Time: $(date)" >> /home/username/time_log.txtNote: It is best to use the full path of the command (you can check with
<span>which echo</span>), and if the command contains a percent sign<span>%</span>, it needs to be escaped. - 3. Save and exit the editor. Cron will automatically load the new configuration.
Summary
| Cron Expression | Meaning |
<span>*/5 * * * *</span> |
Execute every 5 minutes |
<span>0 */5 * * *</span> |
Execute every 5 hours (at the 0th minute of every 5 hours, e.g., 0:00, 5:00, 10:00) |
<span>* * * * *</span> |
Execute every minute |
<span>0 * * * *</span> |
Execute every hour (at the 0th minute of every hour, e.g., 1:00, 2:00) |
Refer to this for creating scheduled tasks in Linux.