0
0
Raspberry Piprogramming~10 mins

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

Choose your learning style9 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.