请求开发轻量Linux脚本:监控日志变更并触发邮件通知
Got it, let's put together a lightweight, reliable solution for your Raspberry Pi 2 running Raspbian/Debian Stretch. This will monitor your GPIO daemon's log file and fire off an email whenever new content is added—no need to include the log lines since you know they're a fixed 24-line repeat.
Step 1: Install Required Tools
First, we'll need two packages: inotify-tools (for efficient file change monitoring, way lighter than polling with tail -f) and mailutils (for sending emails). Run this command to install them:
sudo apt update && sudo apt install -y inotify-tools mailutils
Step 2: Create the Monitoring Script
Make a new script file (let's call it gpio_log_monitor.sh) with the following content. Be sure to replace the placeholders with your actual log path and email recipient:
#!/bin/bash # Configuration - update these values! LOG_FILE="/home/pi/gpio_daemon.log" # Path to your GPIO script's log EMAIL_TO="your-alert-email@example.com" # Email to send notifications to COOLDOWN=60 # Seconds to wait before sending another alert (prevents spam from 24-line writes) # Track when we last sent an email last_alert_time=0 echo "Starting GPIO log monitor... Watching $LOG_FILE" # Monitor the log file for modifications while inotifywait -e modify "$LOG_FILE"; do current_time=$(date +%s) # Only send an alert if we're outside the cooldown window if (( current_time - last_alert_time > COOLDOWN )); then # Send a simple alert email echo "GPIO daemon log has new entries" | mail -s "GPIO Log Update Alert" "$EMAIL_TO" # Update the last alert time last_alert_time=$current_time echo "[$(date '+%Y-%m-%d %H:%M:%S')] Sent alert email to $EMAIL_TO" else echo "[$(date '+%Y-%m-%d %H:%M:%S')] Skipping alert - cooldown active" fi done
Step 3: Configure Email Delivery (If Needed)
If your Pi can't send emails out of the box (common with home setups), configure ssmtp to use an external email provider like Gmail (note: you'll need an app password if using 2FA on your Google account):
- Install ssmtp:
sudo apt install -y ssmtp
- Edit the ssmtp config file:
sudo nano /etc/ssmtp/ssmtp.conf
Replace the contents with this (adjust for your email provider):
root=your-email@gmail.com mailhub=smtp.gmail.com:587 AuthUser=your-email@gmail.com AuthPass=your-google-app-password UseTLS=YES UseSTARTTLS=YES FromLineOverride=YES
- Tell mailutils to use ssmtp:
sudo nano /etc/mail.rc
Add this line at the end:
set sendmail="/usr/sbin/ssmtp -t"
Step 4: Make the Script Run Automatically
- Give the script execute permissions:
chmod +x gpio_log_monitor.sh
- Test it out first to make sure it works:
./gpio_log_monitor.sh &
- To make it start on boot, edit
/etc/rc.local:
sudo nano /etc/rc.local
Add this line right before exit 0 (replace the path with where your script is saved):
/home/pi/gpio_log_monitor.sh &
Quick Notes
- Permissions: Make sure the script has read access to your log file (most logs are readable by all users, but double-check if you've set custom permissions).
- Cooldown Adjustment: The 60-second cooldown prevents multiple alerts from firing as the 24 lines are written. You can tweak this value if needed.
- Check Status: Use
ps aux | grep gpio_log_monitor.shto verify the script is running in the background.
内容的提问来源于stack exchange,提问作者Richarleson Takahashi Neto

