Raspberry Pi β Automation Scripts & Cron Jobs (2025 Scheduling & Automation Guide)
Introduction β Automate Everything with Scripts and Cron on Raspberry Pi
Whether you’re managing sensors, turning on LEDs, backing up files, or rebooting a routerβautomation scripts and cron jobs let your Raspberry Pi handle repetitive tasks on its own. Cron is the native Linux scheduler that runs your scripts at specific times, intervals, or system events.
In this guide, youβll learn:
- How to create and execute automation shell scripts
- How to use
crontabfor time-based scheduling - How to automate hardware, logging, and services
- Best practices to ensure scripts run smoothly
Step 1: Write an Automation Shell Script
Create auto_led.sh
#!/bin/bash
gpio -g mode 17 out
gpio -g write 17 1
sleep 2
gpio -g write 17 0
echo "LED blinked automatically."
Make executable:
chmod +x auto_led.sh
You can now run it anytime:
./auto_led.sh
Step 2: Schedule Scripts with Cron
Open userβs crontab:
crontab -e
Syntax Format:
* * * * * command-to-execute
β β β β β
β β β β ββ Day of week (0β7)
β β β ββββ Month (1β12)
β β ββββββ Day of month (1β31)
β ββββββββ Hour (0β23)
ββββββββββ Minute (0β59)
Example Cron Jobs
| β° Schedule | Command |
|---|---|
| Every 5 minutes | */5 * * * * /home/pi/auto_led.sh |
| Every day at 6:00 PM | 0 18 * * * /home/pi/sysinfo.sh >> log.txt |
| Every reboot | @reboot /home/pi/startup_task.sh |
| Every Sunday backup | 0 2 * * 0 /home/pi/backup.sh |
These examples assume scripts have executable permissions and correct paths.
Automate Raspberry Pi GPIO with Cron
Example: Blink LED Every Hour
0 * * * * /home/pi/blink_led.sh >> /home/pi/logs/led.log 2>&1
Tip: Use >> to append log, 2>&1 to capture errors.
Automate System Monitoring
Create healthcheck.sh
#!/bin/bash
echo "----$(date)----"
vcgencmd measure_temp
df -h
free -m
Add to crontab:
*/10 * * * * /home/pi/healthcheck.sh >> /home/pi/logs/system_health.log
Use it to track system health, detect overheating or low disk space.
Automate File Management
Auto-delete logs older than 7 days:
find /home/pi/logs/ -type f -name "*.log" -mtime +7 -delete
Schedule in crontab:
0 0 * * * /home/pi/cleanup_logs.sh
Keeps your Pi clean and space-efficient.
Common Use Cases for Automation on Raspberry Pi
| Task | Automation Method |
|---|---|
| LED blinking | Shell + GPIO + Cron |
| Sensor logging | Python + CSV + Cron job |
| Camera capture (timelapse) | libcamera-still script every 10 minutes |
| Wi-Fi reconnect | Bash ping-check every 2 minutes |
| System reboot if crashed | Watchdog or cron script check |
| Sync data to cloud | Rsync or rclone scheduled via cron |
Tips for Successful Cron Automation
| Do | Avoid |
|---|---|
| Use full paths in scripts | Using relative paths inside crontab |
| Log output of every script | Running silently and missing errors |
| Test scripts manually first | Scheduling without verifying functionality |
Use @reboot for boot tasks | Expecting rc.local to handle all reliably |
| Organize scripts in folders | Scattering them across home directory |
Advanced Cron Example: Camera Timelapse
* * * * * libcamera-still -o /home/pi/timelapse/image_$(date +\%s).jpg
Captures an image every minute with timestamped filenames.
Summary β Recap & Next Steps
Using shell scripts and cron jobs, your Raspberry Pi can become a fully autonomous systemβcollecting data, controlling devices, and handling errors without human input.
Key takeaways:
- Write reusable
.shscripts and test before scheduling - Use
crontab -eto schedule time or event-based jobs - Automate GPIO, backups, logging, and system health checks
- Use logs and full paths to debug reliably
Real-world relevance: Useful for smart farms, home automation, security monitoring, and self-healing embedded systems.
FAQs β Raspberry Pi Cron and Automation Scripts
How do I check if my cron job ran successfully?
Redirect output to a log file:
/home/pi/myscript.sh >> /home/pi/logs/run.log 2>&1
Then check run.log for results/errors.
My cron job works manually but not in scheduleβwhy?
Use full paths in the script and environment variables. Cron uses a minimal shell environment.
Can I schedule Python scripts with cron?
Yes. Just add:
@hourly /usr/bin/python3 /home/pi/script.py
What is @reboot in cron?
Special cron keyword to run a job once on every system startup.
How do I list all my cron jobs?
Run:
crontab -l
Share Now :
