🧠 5. Raspberry Pi – Programming & Scripting
Estimated reading: 4 minutes 28 views

πŸ§ͺ 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 PM0 18 * * * /home/pi/sysinfo.sh >> log.txt
Every reboot@reboot /home/pi/startup_task.sh
Every Sunday backup0 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 blinkingShell + GPIO + Cron
Sensor loggingPython + CSV + Cron job
Camera capture (timelapse)libcamera-still script every 10 minutes
Wi-Fi reconnectBash ping-check every 2 minutes
System reboot if crashedWatchdog or cron script check
Sync data to cloudRsync or rclone scheduled via cron

🧠 Tips for Successful Cron Automation

βœ… Do⚠️ Avoid
Use full paths in scriptsUsing relative paths inside crontab
Log output of every scriptRunning silently and missing errors
Test scripts manually firstScheduling without verifying functionality
Use @reboot for boot tasksExpecting rc.local to handle all reliably
Organize scripts in foldersScattering 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ§ͺ Raspberry Pi – Automation Scripts & Cron Jobs

Or Copy Link

CONTENTS
Scroll to Top