Scheduled Data Collection with cron on Raspberry Pi
📖 Scenario: You have a Raspberry Pi that collects temperature data from a sensor. You want to save this data into a simple text database file every minute automatically.This project will guide you to set up a cron job that runs a script to append the current temperature and timestamp to a data file.
🎯 Goal: Create a cron job on your Raspberry Pi that runs a script every minute to collect temperature data and save it to a file called temperature_data.txt.
📋 What You'll Learn
Create a bash script called collect_temp.sh that appends the current timestamp and a simulated temperature value to temperature_data.txt.
Make the script executable.
Create a cron job that runs the script every minute.
Verify the cron job is scheduled correctly.
💡 Why This Matters
🌍 Real World
Automating data collection on devices like Raspberry Pi is common in home automation, weather stations, and IoT projects.
💼 Career
Understanding cron jobs and scripting is essential for system administrators, DevOps engineers, and IoT developers to automate tasks and manage data collection.
Progress0 / 4 steps
1
Create the data collection script
Create a bash script called collect_temp.sh that appends the current date and time plus a simulated temperature value (use $((RANDOM % 30 + 10)) to generate a temperature between 10 and 39) to a file named temperature_data.txt. Use the format: YYYY-MM-DD HH:MM:SS Temperature.
Raspberry Pi
Hint
Use date '+%Y-%m-%d %H:%M:%S' to get the current timestamp. Use $((RANDOM % 30 + 10)) to simulate temperature.
2
Make the script executable
Make the collect_temp.sh script executable by adding the correct permission using the chmod command.
Raspberry Pi
Hint
Use chmod +x collect_temp.sh to make the script executable.
3
Schedule the cron job
Add a cron job that runs the collect_temp.sh script every minute. Use crontab -e to edit the cron jobs and add the line: * * * * * /path/to/collect_temp.sh. Replace /path/to/collect_temp.sh with the full path to your script.
Raspberry Pi
Hint
Use * * * * * /full/path/collect_temp.sh to run the script every minute.
4
Verify the cron job setup
Verify that the cron job is scheduled by running crontab -l and ensure the line * * * * * /home/pi/collect_temp.sh appears. Also, check that the temperature_data.txt file is being updated every minute.
Raspberry Pi
Hint
Run crontab -l to list scheduled cron jobs and confirm your job is listed.
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
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 D
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
Step 1: Recall the standard cron command
The command to edit cron jobs is crontab -e, which opens the cron table for editing.
Step 2: Verify other options
Other options like cronedit, cron -edit, and editcron are not valid commands.
Final Answer:
crontab -e -> Option A
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
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 B
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
Step 1: Check cron job command
The command collect_data.sh lacks 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 A
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
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 C