Bird
Raised Fist0
Linux CLIscripting~10 mins

crontab syntax in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - crontab syntax
Start
↓
Read 5 time fields
↓
Match current time with fields?
No→Wait 1 minute
Yes↓
Run scheduled command
↓
Wait 1 minute
↩Back to Read 5 time fields
Crontab reads five time fields, checks if current time matches, runs the command if yes, then waits for the next minute.
Execution Sample
Linux CLI
30 14 * * 1 /home/user/backup.sh
Runs backup.sh at 2:30 PM every Monday.
Execution Table
StepMinute FieldHour FieldDay of Month FieldMonth FieldDay of Week FieldCurrent TimeMatch?Action
13014**114:29 MondayNoWait
23014**114:30 MondayYesRun /home/user/backup.sh
33014**114:31 MondayNoWait
43014**114:30 TuesdayNoWait
💡 Crontab waits each minute, runs command only when all time fields match current time.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
Current Time14:29 Monday14:29 Monday14:30 Monday14:31 Monday14:30 Tuesday
Match ResultNoNoYesNoNo
ActionWaitWaitRun /home/user/backup.shWaitWait
Key Moments - 3 Insights
Why does the command run only at 14:30 on Monday and not other times?
Because the minute field is 30, hour is 14, and day of week is 1 (Monday). Only when all match current time (see execution_table step 2) does the command run.
What does the asterisk (*) mean in the day of month and month fields?
An asterisk means 'any value'. So the command runs regardless of day of month or month, as long as other fields match (see execution_table).
If the day of week is set to 1, will the command run on Tuesday?
No, because day of week 1 means Monday only. On Tuesday, the match fails (see execution_table step 4), so the command does not run.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the action at step 3 when the time is 14:31 Monday?
ARun /home/user/backup.sh
BRun a different command
CWait
DExit crontab
💡 Hint
Check the 'Action' column at step 3 in the execution_table.
At which step does the command run according to the execution table?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look for 'Run /home/user/backup.sh' in the Action column.
If the minute field changed from 30 to 31, when would the command run?
AAt 14:30 Monday
BAt 14:31 Monday
CAt 14:29 Monday
DNever
💡 Hint
Match happens when current time matches all fields; see variable_tracker for Current Time values.
Concept Snapshot
Crontab syntax has 5 time fields: minute hour day_of_month month day_of_week
Each field can be a number or * (any).
Crontab runs the command when current time matches all fields.
Example: '30 14 * * 1' runs at 14:30 every Monday.
Use crontab to schedule repeated tasks easily.
Full Transcript
Crontab syntax uses five fields to schedule commands: minute, hour, day of month, month, and day of week. Each field can be a specific number or an asterisk meaning any value. The crontab checks every minute if the current time matches all fields. If yes, it runs the command. For example, '30 14 * * 1 /home/user/backup.sh' runs the backup script at 2:30 PM every Monday. If the time does not match, crontab waits and checks again the next minute. This way, you can automate tasks to run at specific times easily.

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

  1. 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.
  2. Step 2: Apply to the example

    In * * * * *, each asterisk means every minute, every hour, every day, every month, and every weekday.
  3. Final Answer:

    It means 'every' possible value for that field -> Option D
  4. 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

  1. Step 1: Understand crontab field order

    The fields are: minute, hour, day of month, month, day of week.
  2. 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.
  3. Final Answer:

    30 3 * * * /path/to/script.sh -> Option A
  4. 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?
medium
A. At 2:15 PM on the first day of every month
B. At 2:15 AM on the first day of every month
C. At 2:15 PM every day
D. At 2:15 AM every Monday

Solution

  1. Step 1: Decode the crontab fields

    Fields: minute=15, hour=14 (2 PM), day=1, month=*, weekday=* (any day).
  2. Step 2: Interpret the schedule

    The script runs at 14:15 (2:15 PM) on the 1st day of every month, regardless of weekday.
  3. Final Answer:

    At 2:15 PM on the first day of every month -> Option A
  4. Quick Check:

    Hour=14 means 2 PM, day=1 means first day [OK]
Hint: Hour is 24-hour format; 14 means 2 PM [OK]
Common Mistakes:
  • Confusing 14 as 2 AM instead of 2 PM
  • Ignoring day of month field
  • Assuming runs every day
4. Identify the error in this crontab entry: 60 12 * * 1 /usr/bin/script.sh
medium
A. Day of week cannot be 1; must be 0 or 7 for Monday
B. Hour field cannot be 12; valid range is 0-11
C. Minute field cannot be 60; valid range is 0-59
D. Command path is invalid; must be absolute path

Solution

  1. Step 1: Check minute field range

    Minute field must be between 0 and 59. 60 is invalid.
  2. Step 2: Verify other fields

    Hour=12 is valid (12 PM), day of week=1 is valid (Monday), command path looks absolute.
  3. Final Answer:

    Minute field cannot be 60; valid range is 0-59 -> Option C
  4. Quick Check:

    Minute max is 59, 60 is invalid [OK]
Hint: Minute max is 59, not 60 [OK]
Common Mistakes:
  • Using 60 in minute field
  • Confusing hour ranges
  • Misunderstanding day of week numbers
5. You want to schedule a cleanup script to run every Monday and Friday at 11:45 PM. Which crontab entry is correct?
hard
A. 23 45 * * 1,5 /usr/local/bin/cleanup.sh
B. 45 23 * * 1,5 /usr/local/bin/cleanup.sh
C. 45 11 * * 1,5 /usr/local/bin/cleanup.sh
D. 45 23 1,5 * * /usr/local/bin/cleanup.sh

Solution

  1. Step 1: Understand time and day fields

    11:45 PM means hour=23, minute=45. Monday=1, Friday=5 in day of week field.
  2. Step 2: Match fields to crontab syntax

    Minute=45, hour=23, day of month=*, month=*, day of week=1,5 means Monday and Friday.
  3. Final Answer:

    45 23 * * 1,5 /usr/local/bin/cleanup.sh -> Option B
  4. Quick Check:

    Minute=45, Hour=23, Days=1,5 correct [OK]
Hint: Day of week uses numbers; Monday=1, Friday=5 [OK]
Common Mistakes:
  • Swapping hour and minute
  • Using day of month instead of day of week
  • Wrong day numbers for weekdays
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard