📖 Scenario: You are managing a Linux server and want to schedule tasks to run automatically at specific times. To do this, you will learn how to write crontab entries that tell the system when to run your commands.
🎯 Goal: Build a simple crontab entry that runs a backup script every day at 2:30 AM.
📋 What You'll Learn
Create a crontab line with the correct time fields
Use the exact time 2:30 AM for scheduling
Specify the command to run the backup script located at /usr/local/bin/backup.sh
Print the final crontab line to verify
💡 Why This Matters
🌍 Real World
System administrators use crontab to schedule backups, updates, and maintenance tasks automatically without manual intervention.
💼 Career
Knowing crontab syntax is essential for Linux system administrators, DevOps engineers, and anyone managing servers to automate routine tasks efficiently.
Progress0 / 4 steps
1
Set up the time fields for the crontab entry
Create five variables named minute, hour, day_of_month, month, and day_of_week with these exact values: minute = '30', hour = '2', day_of_month = '*', month = '*', and day_of_week = '*'.
Linux CLI
Hint
Remember, crontab time fields are minute, hour, day of month, month, and day of week.
2
Set the command to run in the crontab entry
Create a variable called command and set it to the string '/usr/local/bin/backup.sh' which is the path to the backup script.
Linux CLI
Hint
The command is the exact path to the script you want to run.
3
Combine the time fields and command into one crontab line
Create a variable called crontab_line that combines minute, hour, day_of_month, month, day_of_week, and command separated by spaces in this exact order: minute, hour, day_of_month, month, day_of_week, command.
Linux CLI
Hint
Use an f-string to join all parts with spaces.
4
Print the final crontab line
Write a print statement to display the value of crontab_line.
Linux CLI
Hint
The output should show the full crontab line exactly.
Practice
(1/5)
1. What does the asterisk (*) symbol mean in a crontab time field like * * * * *?
easy
A. It means 'run only on weekends'
B. It means 'no value' or 'skip this field'
C. It means 'only once at midnight'
D. It means 'every' possible value for that field
Solution
Step 1: Understand the asterisk in crontab
The asterisk (*) in crontab means to match every possible value for that time field, like every minute or every hour.
Step 2: Apply to the example
In * * * * *, each asterisk means every minute, every hour, every day, every month, and every weekday.
Final Answer:
It means 'every' possible value for that field -> Option D
Quick Check:
Asterisk = every value [OK]
Hint: Asterisk means 'every' in crontab fields [OK]
Common Mistakes:
Thinking * means 'no value' or skip
Confusing * with specific times
Assuming * means only weekends
2. Which of the following is the correct crontab syntax to run a script every day at 3:30 AM?
easy
A. 30 3 * * * /path/to/script.sh
B. 3 30 * * * /path/to/script.sh
C. * 3 30 * * /path/to/script.sh
D. 30 * 3 * * /path/to/script.sh
Solution
Step 1: Understand crontab field order
The fields are: minute, hour, day of month, month, day of week.
Step 2: Match time 3:30 AM
3:30 AM means minute=30, hour=3, so the first two fields must be 30 and 3 respectively.
Final Answer:
30 3 * * * /path/to/script.sh -> Option A
Quick Check:
Minute=30, Hour=3 correct [OK]
Hint: Minute is first, hour second in crontab [OK]
Common Mistakes:
Swapping hour and minute fields
Using day or month fields incorrectly
Misplacing asterisks
3. Given the crontab entry 15 14 1 * * running /usr/bin/backup.sh, when will the script run?