Bird
Raised Fist0
Raspberry Piprogramming~10 mins

Cron jobs for scheduled tasks in Raspberry Pi - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Cron jobs for scheduled tasks
Write cron schedule line
↓
Save to crontab file
↓
Cron daemon reads schedule
↓
Wait for scheduled time
↓
Execute task command
↓
Task runs in background
↓
Repeat at next scheduled time
Cron jobs run commands at set times by reading schedule lines and executing tasks automatically.
Execution Sample
Raspberry Pi
*/1 * * * * /home/pi/script.sh
# Runs script.sh every minute
This cron job runs the script.sh file every minute automatically.
Execution Table
StepTimeCron Schedule MatchActionOutput
112:00:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
212:01:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
312:02:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
412:03:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
512:04:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
612:05:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
712:06:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
812:07:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
912:08:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
1012:09:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
1112:10:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
1212:11:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
1312:12:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
1412:13:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
1512:14:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
1612:15:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
1712:16:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
1812:17:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
1912:18:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
2012:19:00Matches */1 * * * *Run /home/pi/script.shScript runs, output logged
Exit12:20:00Stop tracing after 20 runsNo further actionEnd of trace
💡 Trace stops after 20 runs to show repeated execution every minute.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
Current Time12:00:0012:01:0012:02:0012:03:0012:04:0012:05:0012:20:00
Cron MatchTrueTrueTrueTrueTrueTrueTrue
Task Run Count01234520
Key Moments - 3 Insights
Why does the cron job run every minute instead of just once?
Because the schedule uses '*/1' in the minute field, which means every minute. See execution_table rows 1-20 where the task runs repeatedly.
What happens if the script takes longer than a minute to run?
Cron will still start a new instance at the next scheduled time, possibly causing overlap. This is why scripts should finish quickly or handle concurrency.
How does cron know when to run the task?
Cron daemon continuously checks the current time against the schedule lines saved in crontab. When they match, it runs the task. See concept_flow step 'Cron daemon reads schedule'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the 'Task Run Count' after step 5?
A5
B4
C6
D1
💡 Hint
Check variable_tracker row 'Task Run Count' column 'After 5'
At which time does the cron job first run according to the execution_table?
A12:01:00
B12:00:00
C12:05:00
D12:20:00
💡 Hint
Look at execution_table row 1 under 'Time'
If the cron schedule changed to '*/5 * * * *', how would the execution_table change?
ATask runs every second
BTask runs only once
CTask runs every 5 minutes instead of every minute
DTask runs every hour
💡 Hint
Understand the meaning of '*/5' in the minute field from concept_flow
Concept Snapshot
Cron jobs run commands automatically at scheduled times.
Schedule lines have 5 fields: minute, hour, day, month, weekday.
Use crontab to edit and save schedules.
Cron daemon checks time and runs matching tasks.
Example: '*/1 * * * *' runs every minute.
Tasks run in background without user action.
Full Transcript
Cron jobs let you run commands automatically on your Raspberry Pi at set times. You write a schedule line with five parts: minute, hour, day, month, and weekday. For example, '*/1 * * * *' means every minute. You save this line in the crontab file. The cron daemon reads this schedule and waits. When the current time matches the schedule, cron runs your command or script in the background. This repeats as scheduled. The execution table shows the task running every minute from 12:00 to 12:20. Variables like current time and run count update each minute. Beginners often wonder why the task runs repeatedly; it's because of the '*/1' minute setting. If the script runs longer than a minute, cron still starts new runs on time, so scripts should be quick or handle overlaps. Changing the schedule changes how often the task runs. Cron is a simple but powerful way to automate tasks on your Pi.

Practice

(1/5)
1. What is the main purpose of a cron job on a Raspberry Pi?
easy
A. To update the Raspberry Pi operating system
B. To manually execute commands in the terminal
C. To schedule tasks to run automatically at specific times
D. To install new software packages

Solution

  1. Step 1: Understand what cron jobs do

    Cron jobs are used to automate tasks by running commands or scripts at scheduled times without manual intervention.
  2. Step 2: Compare options with cron job purpose

    Only To schedule tasks to run automatically at specific times describes scheduling tasks automatically, which matches the purpose of cron jobs.
  3. Final Answer:

    To schedule tasks to run automatically at specific times -> Option C
  4. Quick Check:

    Cron job purpose = schedule tasks automatically [OK]
Hint: Cron jobs run commands automatically on schedule [OK]
Common Mistakes:
  • Confusing cron jobs with manual command execution
  • Thinking cron updates the system automatically
  • Assuming cron installs software
2. Which of the following is the correct syntax to edit the cron jobs for the current user on a Raspberry Pi?
easy
A. cronedit
B. cron -edit
C. editcron
D. crontab -e

Solution

  1. Step 1: Recall the command to edit cron jobs

    The standard command to edit cron jobs for the current user is crontab -e.
  2. Step 2: Verify other options

    Options A, B, and D are not valid Linux commands for editing cron jobs.
  3. Final Answer:

    crontab -e -> Option D
  4. Quick Check:

    Edit cron jobs command = crontab -e [OK]
Hint: Use 'crontab -e' to edit cron jobs [OK]
Common Mistakes:
  • Typing incorrect commands like 'cronedit'
  • Using 'cron -edit' which is invalid
  • Assuming 'editcron' is a command
3. Given the cron job entry 30 2 * * * /home/pi/backup.sh, what does it do?
medium
A. Runs backup.sh every day at 2:30 AM
B. Runs backup.sh every 30 minutes
C. Runs backup.sh every day at 2:00 PM
D. Runs backup.sh every 2 hours and 30 minutes

Solution

  1. Step 1: Understand cron time fields

    The fields are: minute (30), hour (2), day of month (*), month (*), day of week (*). This means the command runs at 2:30 AM every day.
  2. Step 2: Match the time to options

    Runs backup.sh every day at 2:30 AM correctly states it runs daily at 2:30 AM. Other options misunderstand the timing.
  3. Final Answer:

    Runs backup.sh every day at 2:30 AM -> Option A
  4. Quick Check:

    Cron time 30 2 * * * = 2:30 AM daily [OK]
Hint: Minute then hour in cron means 30 2 is 2:30 AM [OK]
Common Mistakes:
  • Confusing AM and PM hours
  • Thinking it runs every 30 minutes
  • Misreading the asterisks as specific days
4. You added this cron job: 0 25 * * * /home/pi/script.sh but it never runs. What is the problem?
medium
A. The hour value 25 is invalid in cron syntax
B. The script path is incorrect
C. The minute value 0 is invalid
D. Cron jobs cannot run scripts

Solution

  1. Step 1: Check the hour field in cron syntax

    The hour field must be between 0 and 23. 25 is invalid and causes the job not to run.
  2. Step 2: Verify other options

    The script path might be correct, minute 0 is valid, and cron can run scripts, so only the hour value is the problem.
  3. Final Answer:

    The hour value 25 is invalid in cron syntax -> Option A
  4. Quick Check:

    Cron hour must be 0-23, 25 is invalid [OK]
Hint: Hour in cron is 0-23, not 25 [OK]
Common Mistakes:
  • Using invalid hour values above 23
  • Assuming minute 0 is wrong
  • Thinking cron can't run scripts
5. You want to run a Python script every weekday at 7:15 AM on your Raspberry Pi. Which cron job entry is correct?
hard
A. 7 15 * * 1-5 /usr/bin/python3 /home/pi/script.py
B. 15 7 * * 1-5 /usr/bin/python3 /home/pi/script.py
C. 15 7 * * 6,7 /usr/bin/python3 /home/pi/script.py
D. 15 7 1-5 * * /usr/bin/python3 /home/pi/script.py

Solution

  1. Step 1: Understand cron fields for time and days

    The format is minute hour day_of_month month day_of_week. To run at 7:15 AM, minute=15, hour=7. Weekdays are days 1-5 (Monday to Friday).
  2. Step 2: Check each option

    15 7 * * 1-5 /usr/bin/python3 /home/pi/script.py correctly uses '15 7 * * 1-5' for 7:15 AM on weekdays. 7 15 * * 1-5 /usr/bin/python3 /home/pi/script.py swaps hour and minute. 15 7 * * 6,7 /usr/bin/python3 /home/pi/script.py uses weekend days (6,7). 15 7 1-5 * * /usr/bin/python3 /home/pi/script.py uses day_of_month 1-5, not weekdays.
  3. Final Answer:

    15 7 * * 1-5 /usr/bin/python3 /home/pi/script.py -> Option B
  4. Quick Check:

    Weekday cron = 1-5, time 7:15 = 15 7 [OK]
Hint: Weekdays are 1-5 in cron day_of_week field [OK]
Common Mistakes:
  • Swapping hour and minute fields
  • Using day_of_month instead of day_of_week for weekdays
  • Confusing weekend days with 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