📖 Scenario: You are managing a Linux server and need to schedule tasks to run automatically at specific times. Cron jobs help you do this by using cron expressions that tell the system when to run your commands.Understanding common cron expressions will help you automate daily, weekly, and monthly tasks easily.
🎯 Goal: Learn to write common cron expressions for scheduling tasks at specific times and intervals.You will create cron expressions for running commands every minute, hourly, daily at midnight, weekly on Sunday at midnight, and monthly on the first day at midnight.
📋 What You'll Learn
Create variables with exact cron expressions as strings
Use the exact variable names: every_minute, hourly, daily_midnight, weekly_sunday_midnight, monthly_first_midnight
Print each variable to show the cron expression
💡 Why This Matters
🌍 Real World
System administrators and developers use cron expressions to automate backups, updates, and maintenance tasks on servers.
💼 Career
Knowing common cron expressions is essential for roles involving Linux server management, DevOps, and automation scripting.
Progress0 / 4 steps
1
Create cron expression for every minute
Create a variable called every_minute and set it to the string "* * * * *" which means run every minute.
Linux CLI
Hint
This cron expression uses five asterisks separated by spaces to mean every minute of every hour, day, month, and weekday.
2
Add cron expressions for hourly and daily tasks
Add two variables: hourly set to "0 * * * *" to run at the start of every hour, and daily_midnight set to "0 0 * * *" to run daily at midnight.
Linux CLI
Hint
Use zero for the minute field to run at the start of the hour or day.
3
Add cron expressions for weekly and monthly tasks
Add two variables: weekly_sunday_midnight set to "0 0 * * 0" to run weekly on Sunday at midnight, and monthly_first_midnight set to "0 0 1 * *" to run monthly on the first day at midnight.
Linux CLI
Hint
The last field is the day of the week (0 for Sunday). The third field is the day of the month.
4
Print all cron expressions
Print the variables every_minute, hourly, daily_midnight, weekly_sunday_midnight, and monthly_first_midnight each on its own line.
Linux CLI
Hint
Use separate print statements for each variable.
Practice
(1/5)
1. What does the cron expression 0 0 * * * do?
easy
A. Runs a task every minute
B. Runs a task every hour
C. Runs a task every day at midnight
D. Runs a task every Sunday at midnight
Solution
Step 1: Understand the cron fields
The expression has five fields: minute=0, hour=0, day=*, month=*, weekday=*. This means minute 0, hour 0, every day, every month, every weekday.
Step 2: Interpret the schedule
Since minute and hour are fixed at 0, and others are '*', the task runs once daily at 00:00 (midnight).
Final Answer:
Runs a task every day at midnight -> Option C
Quick Check:
0 0 * * * = daily at midnight [OK]
Hint: Minute and hour 0 means daily at midnight [OK]
Common Mistakes:
Thinking * means no schedule
Confusing hour and minute fields
Assuming it runs every minute
2. Which of the following is the correct cron syntax to run a job every 15 minutes?
easy
A. 15 * * * *
B. */15 * * * *
C. * 15 * * *
D. 0 */15 * * *
Solution
Step 1: Understand the minute field syntax
Using '*/15' in the minute field means every 15 minutes (0,15,30,45).
Step 2: Check other fields
All other fields are '*', meaning every hour, day, month, and weekday.
Final Answer:
*/15 * * * * -> Option B
Quick Check:
*/15 in minutes means every 15 minutes [OK]
Hint: */15 in minute field means every 15 minutes [OK]
Common Mistakes:
Using 15 in minute field runs only at minute 15
Putting 15 in hour field runs at 3 PM only
Using 0 */15 runs every 15 hours, not minutes
3. What is the output behavior of this cron expression: 30 6 * * 1?