Bird
Raised Fist0
Raspberry Piprogramming~20 mins

Scheduled data collection with cron 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!
❓ query_result
intermediate
1:30remaining
What is the output of this cron schedule?
Given the cron expression 30 2 * * 1-5, when will the scheduled task run?
AAt 2:30 AM every weekday (Monday to Friday)
BAt 2:30 PM every day
CAt 2:00 AM every Monday to Friday
DAt 30 minutes past every hour on weekdays
Attempts:
2 left
💡 Hint
Remember the order of fields in a cron expression: minute, hour, day of month, month, day of week.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in this cron job entry
Which option shows a syntax error in a cron job entry?
A0 25 * * * /home/pi/script.sh
B0 0 * * 0 /home/pi/script.sh
C15 14 1 * * /home/pi/script.sh
D*/10 * * * * /home/pi/script.sh
Attempts:
2 left
💡 Hint
Check the valid range for the hour field in cron.
❓ optimization
advanced
2:00remaining
Optimize this cron schedule for hourly data collection
You want to collect data every hour at 15 minutes past the hour. Which cron expression is the most efficient and correct?
A*/15 * * * * /home/pi/collect_data.sh
B0 15 * * * /home/pi/collect_data.sh
C15 * * * * /home/pi/collect_data.sh
D15 0-23 * * * /home/pi/collect_data.sh
Attempts:
2 left
💡 Hint
Remember the order: minute, hour, day, month, weekday.
🔧 Debug
advanced
2:00remaining
Why does this cron job not run as expected?
A cron job entry is: 0 6 * * * /home/pi/data_collect.sh. The script runs fine manually but does not run at 6 AM. What is the most likely cause?
AThe script path is incorrect because cron uses a different working directory
BCron does not support running scripts at 6 AM
CThe cron daemon is not installed on Raspberry Pi
DThe script lacks execute permissions for the user running cron
Attempts:
2 left
💡 Hint
Check permissions and environment differences between manual and cron runs.
🧠 Conceptual
expert
3:00remaining
How to ensure data collection runs even if the previous run is still active?
You schedule a data collection script every 10 minutes with cron. Sometimes the script takes longer than 10 minutes. Which approach ensures no overlapping runs?
ARun multiple instances in parallel to speed up data collection
BUse a lock file in the script to check if a previous instance is running and exit if so
CRestart the Raspberry Pi before each scheduled run
DSchedule the cron job every 5 minutes instead of 10
Attempts:
2 left
💡 Hint
Think about how to prevent multiple script instances from running simultaneously.

Practice

(1/5)
1. What is the main purpose of using cron on a Raspberry Pi for database tasks?
easy
A. To create database backups only when the Pi is restarted
B. To manually start database queries
C. To store data permanently
D. To automatically run scripts at scheduled times

Solution

  1. Step 1: Understand cron's role

    Cron is a tool that runs commands or scripts automatically at set times without user intervention.
  2. Step 2: Connect cron to database tasks

    Using cron, you can schedule database scripts to run regularly, like collecting data or backups.
  3. Final Answer:

    To automatically run scripts at scheduled times -> Option D
  4. Quick Check:

    Cron automates tasks = A [OK]
Hint: Cron schedules tasks automatically, no manual start needed [OK]
Common Mistakes:
  • Thinking cron runs scripts only manually
  • Confusing cron with data storage
  • Assuming cron runs only at reboot
2. Which of the following is the correct command to edit the cron jobs on a Raspberry Pi?
easy
A. crontab -e
B. cronedit
C. cron -edit
D. editcron

Solution

  1. Step 1: Recall the standard cron command

    The command to edit cron jobs is crontab -e, which opens the cron table for editing.
  2. Step 2: Verify other options

    Other options like cronedit, cron -edit, and editcron are not valid commands.
  3. Final Answer:

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

    Edit cron jobs = crontab -e [OK]
Hint: Use 'crontab -e' to edit cron jobs every time [OK]
Common Mistakes:
  • Typing 'cronedit' instead of 'crontab -e'
  • Using wrong command syntax
  • Confusing cron commands with other tools
3. Given this cron job entry:
0 * * * * /home/pi/scripts/collect_data.sh
What does this cron job do?
medium
A. Runs the script every minute
B. Runs the script every hour at minute 0
C. Runs the script once a day at midnight
D. Runs the script every day at noon

Solution

  1. Step 1: Understand cron time format

    The five fields represent minute, hour, day of month, month, and day of week. '0 * * * *' means minute 0 of every hour.
  2. Step 2: Interpret the schedule

    This means the script runs at the start of every hour (e.g., 1:00, 2:00, 3:00, etc.).
  3. Final Answer:

    Runs the script every hour at minute 0 -> Option B
  4. Quick Check:

    Minute 0 every hour = hourly run [OK]
Hint: Minute 0 with * hour means hourly at start [OK]
Common Mistakes:
  • Thinking it runs every minute
  • Confusing hour and minute fields
  • Assuming daily or noon run
4. A user added this cron job:
*/15 * * * * collect_data.sh
But the script does not run. What is the most likely problem?
medium
A. The script path is missing; cron needs full path
B. The cron syntax is invalid
C. The script runs only once a day
D. Cron does not support scripts with .sh extension

Solution

  1. Step 1: Check cron job command

    The command collect_data.sh lacks a full path. Cron requires full paths to scripts.
  2. Step 2: Understand cron environment

    Cron runs with a minimal environment and does not know where to find scripts without full paths.
  3. Final Answer:

    The script path is missing; cron needs full path -> Option A
  4. Quick Check:

    Always use full script path in cron [OK]
Hint: Always specify full script path in cron jobs [OK]
Common Mistakes:
  • Assuming cron finds scripts in current directory
  • Thinking .sh extension is unsupported
  • Believing syntax is wrong when it is correct
5. You want to collect sensor data every day at 6:30 AM and save it to a database using a script at /home/pi/scripts/save_data.sh. Which cron entry correctly schedules this?
hard
A. 30 06 * * 1-5 /home/pi/scripts/save_data.sh
B. 6 30 * * * /home/pi/scripts/save_data.sh
C. 30 6 * * * /home/pi/scripts/save_data.sh
D. 0 6 * * * /home/pi/scripts/save_data.sh

Solution

  1. Step 1: Understand cron time fields

    The format is: minute hour day month weekday. To run at 6:30 AM daily, minute=30 and hour=6.
  2. Step 2: Check each option

    30 6 * * * /home/pi/scripts/save_data.sh uses '30 6 * * *' which means 6:30 AM every day. 6 30 * * * /home/pi/scripts/save_data.sh reverses minute and hour. 30 06 * * 1-5 /home/pi/scripts/save_data.sh restricts to weekdays only. 0 6 * * * /home/pi/scripts/save_data.sh runs at 6:00 AM.
  3. Final Answer:

    30 6 * * * /home/pi/scripts/save_data.sh -> Option C
  4. Quick Check:

    Minute 30, hour 6 daily = 30 6 * * * /home/pi/scripts/save_data.sh [OK]
Hint: Minute first, then hour in cron time fields [OK]
Common Mistakes:
  • Swapping hour and minute fields
  • Forgetting to specify full script path
  • Limiting days unintentionally
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