Scheduled data collection with cron in Raspberry Pi - Time & Space Complexity
When we schedule data collection tasks on a Raspberry Pi using cron, it's important to understand how the time it takes to run these tasks grows as we collect more data.
We want to know how the work done changes when the amount of data increases.
Analyze the time complexity of the following cron job script that collects sensor data and stores it in a database.
#!/bin/bash
# Collect sensor data and save to database
for sensor_id in $(seq 1 100); do
data=$(python3 read_sensor.py $sensor_id)
sqlite3 sensors.db "INSERT INTO readings (sensor_id, value) VALUES ($sensor_id, $data);"
done
This script reads data from 100 sensors one by one and inserts each reading into a database.
Look at what repeats in the script.
- Primary operation: Loop over 100 sensors to read and insert data.
- How many times: Exactly 100 times, once per sensor.
As the number of sensors increases, the script runs more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 sensor reads and inserts |
| 100 | 100 sensor reads and inserts |
| 1000 | 1000 sensor reads and inserts |
Pattern observation: The work grows directly with the number of sensors. Double the sensors, double the work.
Time Complexity: O(n)
This means the time to collect and store data grows in a straight line as the number of sensors increases.
[X] Wrong: "The script runs instantly no matter how many sensors there are."
[OK] Correct: Each sensor reading and database insert takes time, so more sensors mean more total time.
Understanding how scheduled tasks scale helps you design efficient data collection systems on devices like Raspberry Pi, a useful skill in many real projects.
What if we changed the script to read sensors in parallel instead of one by one? How would the time complexity change?
Practice
cron on a Raspberry Pi for database tasks?Solution
Step 1: Understand cron's role
Cron is a tool that runs commands or scripts automatically at set times without user intervention.Step 2: Connect cron to database tasks
Using cron, you can schedule database scripts to run regularly, like collecting data or backups.Final Answer:
To automatically run scripts at scheduled times -> Option DQuick Check:
Cron automates tasks = A [OK]
- Thinking cron runs scripts only manually
- Confusing cron with data storage
- Assuming cron runs only at reboot
Solution
Step 1: Recall the standard cron command
The command to edit cron jobs iscrontab -e, which opens the cron table for editing.Step 2: Verify other options
Other options likecronedit,cron -edit, andeditcronare not valid commands.Final Answer:
crontab -e -> Option AQuick Check:
Edit cron jobs = crontab -e [OK]
- Typing 'cronedit' instead of 'crontab -e'
- Using wrong command syntax
- Confusing cron commands with other tools
0 * * * * /home/pi/scripts/collect_data.shWhat does this cron job do?
Solution
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.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.).Final Answer:
Runs the script every hour at minute 0 -> Option BQuick Check:
Minute 0 every hour = hourly run [OK]
- Thinking it runs every minute
- Confusing hour and minute fields
- Assuming daily or noon run
*/15 * * * * collect_data.shBut the script does not run. What is the most likely problem?
Solution
Step 1: Check cron job command
The commandcollect_data.shlacks a full path. Cron requires full paths to scripts.Step 2: Understand cron environment
Cron runs with a minimal environment and does not know where to find scripts without full paths.Final Answer:
The script path is missing; cron needs full path -> Option AQuick Check:
Always use full script path in cron [OK]
- Assuming cron finds scripts in current directory
- Thinking .sh extension is unsupported
- Believing syntax is wrong when it is correct
/home/pi/scripts/save_data.sh. Which cron entry correctly schedules this?Solution
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.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.Final Answer:
30 6 * * * /home/pi/scripts/save_data.sh -> Option CQuick Check:
Minute 30, hour 6 daily = 30 6 * * * /home/pi/scripts/save_data.sh [OK]
- Swapping hour and minute fields
- Forgetting to specify full script path
- Limiting days unintentionally
Ready to prove it?
Pass the test and this pattern moves unknown
