Bird
Raised Fist0
Raspberry Piprogramming~20 mins

Cron jobs for scheduled tasks in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cron Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
What is the output of this cron schedule expression?
Given the cron schedule expression 30 14 * * 1-5, when will the cron job run?
AAt 2:30 PM every Saturday and Sunday
BAt 2:30 PM every weekday (Monday to Friday)
CAt 2:30 AM every day
DAt 14:00 every weekday (Monday to Friday)
Attempts:
2 left
💡 Hint
Remember, the fields are: minute, hour, day of month, month, day of week.
❓ Predict Output
intermediate
1:30remaining
What will this cron job output?
A cron job runs the command echo $(date +"%H:%M") > /home/pi/time.txt every hour at minute 0. What will be the content of time.txt after the job runs at 3 PM?
A15:00
B03:00
C3 PM
DThe current date and time in full
Attempts:
2 left
💡 Hint
The format string %H:%M gives hour in 24-hour format and minutes.
🔧 Debug
advanced
2:00remaining
Why does this cron job not run?
A user added this line to their crontab: 0 9 * * * /home/pi/myscript.sh. The script has execute permission and runs fine manually. But the cron job never runs. What is the most likely reason?
AThe script path is incorrect or missing environment variables in cron
BThe cron job syntax is invalid
CThe script does not have execute permission
DThe cron daemon is not installed on Raspberry Pi
Attempts:
2 left
💡 Hint
Cron jobs run with a limited environment and may not have the same PATH as your user.
🧠 Conceptual
advanced
1:30remaining
How to schedule a cron job to run every 15 minutes?
Which cron schedule expression correctly runs a job every 15 minutes?
A0,15,30,45 * * * *
B15 * * * *
C*/15 * * * *
D0-15 * * * *
Attempts:
2 left
💡 Hint
The slash (/) operator in cron means 'every'.
📝 Syntax
expert
1:30remaining
What error does this crontab line cause?
Consider this crontab line: 0 25 * * * /home/pi/script.sh. What happens when cron tries to run this?
AThe cron daemon ignores the line and runs other jobs
BThe job runs at 1 AM (25 mod 24)
CThe job runs at midnight
DThe cron job will not run because hour 25 is invalid
Attempts:
2 left
💡 Hint
Hours in cron must be between 0 and 23.

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