π§ͺ 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
crontab
for 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
.sh
scripts and test before scheduling - Use
crontab -e
to 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 :