Common cron expressions in Linux CLI - Time & Space Complexity
We want to understand how the time it takes to run cron jobs grows as we schedule more tasks or more frequent jobs.
How does the number of cron expressions affect the system's work over time?
Analyze the time complexity of checking and running cron jobs with these expressions.
* * * * * /path/to/script1.sh
0 * * * * /path/to/script2.sh
0 0 * * * /path/to/script3.sh
*/15 * * * * /path/to/script4.sh
0 0 1 * * /path/to/script5.sh
This snippet shows common cron expressions that schedule scripts at different intervals: every minute, hourly, daily, every 15 minutes, and monthly.
Each cron expression causes the system to check if it should run a job at each minute.
- Primary operation: Checking each cron expression every minute.
- How many times: Once per minute for each expression.
As the number of cron expressions (n) grows, the system checks each one every minute.
| Input Size (n) | Approx. Checks per hour |
|---|---|
| 10 | 600 |
| 100 | 6000 |
| 1000 | 60000 |
Pattern observation: The number of checks grows directly with the number of cron expressions.
Time Complexity: O(n)
This means the work grows linearly with the number of cron expressions to check each minute.
[X] Wrong: "Cron jobs run only when scheduled, so adding more jobs doesn't increase checking work."
[OK] Correct: The system checks all cron expressions every minute to decide which jobs to run, so more jobs mean more checks.
Understanding how scheduled tasks scale helps you design systems that stay efficient as they grow. This skill shows you can think about system workload over time.
What if cron expressions were grouped and checked in batches instead of individually? How would the time complexity change?
Practice
0 0 * * * do?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 CQuick Check:
0 0 * * * = daily at midnight [OK]
- Thinking * means no schedule
- Confusing hour and minute fields
- Assuming it runs every minute
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 BQuick Check:
*/15 in minutes means every 15 minutes [OK]
- 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
30 6 * * 1?Solution
Step 1: Analyze the fields
Minute=30, Hour=6, Day=*, Month=*, Weekday=1 (Monday).Step 2: Interpret the schedule
The task runs at 6:30 AM only on Mondays.Final Answer:
Runs at 6:30 AM every Monday -> Option AQuick Check:
30 6 * * 1 = Monday 6:30 AM [OK]
- Confusing weekday with day of month
- Assuming it runs daily instead of weekly
- Mixing hour and minute fields
60 24 * * *Solution
Step 1: Check minute field validity
Minute field must be 0-59; 60 is invalid.Step 2: Check hour field validity
Hour field must be 0-23; 24 is invalid.Final Answer:
Both minute and hour values are invalid -> Option DQuick Check:
Minute max 59, hour max 23 [OK]
- Assuming 60 is valid minute
- Assuming 24 is valid hour
- Thinking cron allows 24:00 time
Solution
Step 1: Map time and date to cron fields
Minute=15, Hour=2, Day=1 (first day), Month=*, Weekday=* (any day).Step 2: Verify expression correctness
Expression '15 2 1 * *' matches 2:15 AM on day 1 of every month.Final Answer:
15 2 1 * * -> Option AQuick Check:
Minute=15, Hour=2, Day=1 means 2:15 AM on 1st [OK]
- Swapping hour and minute fields
- Using weekday instead of day of month
- Setting wrong day or time values
Ready to prove it?
Pass the test and this pattern moves unknown
