0
0
Linux CLIscripting~10 mins

Common cron expressions in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Common cron expressions
Start
Read cron expression
Parse fields: min, hour, day, month, weekday
Match current time with fields
If all match?
NoWait for next minute
Yes
Run scheduled command
Repeat every minute
Cron reads the expression fields, checks if current time matches, and runs the command if all fields match.
Execution Sample
Linux CLI
*/5 * * * * echo "Hello"
0 0 * * 0 /backup.sh
30 14 1 * * /monthly_report.sh
These cron lines run commands every 5 minutes, weekly on Sunday midnight, and monthly on the 1st at 14:30.
Execution Table
StepCron FieldValueCurrent TimeMatch?Action
1Minute*/500YesCheck next field
2Hour*14YesCheck next field
3Day of Month*1YesCheck next field
4Month*6YesCheck next field
5Day of Week*3 (Wednesday)YesRun command: echo "Hello"
6Minute000YesCheck next field
7Hour000YesCheck next field
8Day of Month*1YesCheck next field
9Month*6YesCheck next field
10Day of Week0 (Sunday)3 (Wednesday)NoDo not run /backup.sh
11Minute3030YesCheck next field
12Hour1414YesCheck next field
13Day of Month11YesCheck next field
14Month*6YesCheck next field
15Day of Week*3 (Wednesday)YesRun command: /monthly_report.sh
16EndWait for next minute
💡 Execution stops after checking all fields and running commands if matched, then waits for next minute.
Variable Tracker
VariableStartAfter 1After 2After 3Final
MinuteCurrent time00003030
HourCurrent time14141414
Day of MonthCurrent time1111
MonthCurrent time6666
Day of WeekCurrent time3333
Command RunNoneecho "Hello"None/monthly_report.shNone
Key Moments - 3 Insights
Why does the /backup.sh command not run even though the minute and hour match?
Because the day of week field is 0 (Sunday) but current day is 3 (Wednesday), so the condition fails at step 10 in the execution table.
What does the '*/5' in the minute field mean?
It means every 5 minutes, so at minutes 0, 5, 10, 15, etc. This is shown in step 1 where minute 00 matches '*/5'.
Can a cron job run if only some fields match?
No, all fields must match the current time for the command to run, as shown by the checks in the execution table steps.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the /backup.sh command fail to run?
AStep 5
BStep 15
CStep 10
DStep 1
💡 Hint
Check the 'Match?' column for /backup.sh in the execution table rows.
According to the variable tracker, what is the value of 'Minute' after the third check?
A30
B00
C14
D3
💡 Hint
Look at the 'Minute' row in variable_tracker under 'After 3'.
If the day of week for /backup.sh was changed to 3, what would happen?
AThe command would run every minute
BThe command would run at midnight on Wednesday
CThe command would never run
DThe command would run only on Sundays
💡 Hint
Refer to the execution table where day of week mismatch caused no run at step 10.
Concept Snapshot
Cron expressions have 5 fields: minute, hour, day of month, month, day of week.
Each field can be a number, '*', or step values like '*/5'.
Cron runs the command only if all fields match the current time.
Common patterns: '*/5 * * * *' runs every 5 minutes.
'0 0 * * 0' runs weekly on Sunday at midnight.
'30 14 1 * *' runs monthly on the 1st at 14:30.
Full Transcript
Cron expressions consist of five fields: minute, hour, day of month, month, and day of week. The cron daemon checks each field against the current time every minute. If all fields match, it runs the scheduled command. For example, '*/5 * * * *' runs a command every 5 minutes. '0 0 * * 0' runs a command weekly on Sunday at midnight. '30 14 1 * *' runs a command monthly on the first day at 14:30. If any field does not match, the command does not run. This is shown in the execution table where the /backup.sh command does not run because the day of week does not match. Understanding each field and how they match current time is key to using cron effectively.