
scripts for Synology DSM
How do I run scripts from Synology Task Scheduler and how do I create scripts?
Click here for instructions how to create scripts and run those scripts from the Synology Task Scheduler in Control Panel.
Warning
Warning: always test scripts in a testing environment before usage, use at your own risk
This version: read S.M.A.R.T. attributes and send email notification when error
prerequisites:
- You have smartctl installed (usually available on Synology via smartmontools).
- Make sure sendmail is configured and working on your NAS (it uses Synology MailPlus settings)
What You’ll Get:
- SMART attribute checks
- Disk tagging (Model, Serial, Firmware)
- Email notification with subject based on error presence
- Logs with timestamps
- Compatible with Synology Task Scheduler
Script:
#!/bin/bash
# Hostname for Email Config
HOSTNAS=$(hostname)
# Full paths for reliability in Task Scheduler
SMARTCTL=”/usr/bin/smartctl”
SENDMAIL=”/usr/sbin/sendmail”
# Set your TMPFILE and LOGFILE
DATESTAMP=$(date ‘+%Y-%m-%d_%H-%M-%S’)
TMPFILE=”/volume1/log/smart/smart_report_$DATESTAMP.txt”
LOGFILE=”/volume1/log/smart/smart_log.txt”
# Email config
TO=”monitor@example.com”
FROM=”nas@example.com”
SUBJECT_OK=”SMART Report “$HOSTNAS”: No Issues Detected”
SUBJECT_ALERT=”SMART Alert “$HOSTNAS”: Issues Detected on NAS”
# Initialize
alert_triggered=0
echo “SMART Report – $DATESTAMP” > “$TMPFILE”
echo “—————————————-” >> “$TMPFILE”
# Get all /dev/sd? and /dev/sata? devices
disks=$(ls /dev/sd? /dev/sata? 2>/dev/null)
for disk in $disks; do
echo “Checking $disk…” >> “$TMPFILE”
if $SMARTCTL -A -H -i -d sat -T permissive “$disk” &>/dev/null; then
output=$($SMARTCTL -A -H -i -d sat -T permissive “$disk”)
# Get disk model, serial number, and firmware version
model=$(echo “$output” | grep -i “Device Model” | cut -d ‘:’ -f2- | xargs)
serial=$(echo “$output” | grep -i “Serial Number” | cut -d ‘:’ -f2- | xargs)
firmware=$(echo “$output” | grep -i “Firmware Version” | cut -d ‘:’ -f2- | xargs)
# Add tagging to the report
echo “$disk – Model: $model | Serial: $serial | Firmware: $firmware” >> “$TMPFILE”
# Define SMART attributes to check
attributes=(
“Read_Error_Rate”
“Reallocated_Sector_Ct”
“Spin_Retry_Count”
“End-to-End_Error”
“Reallocated_Event_Count”
“Current_Pending_Sector”
“Offline_Uncorrectable”
“UDMA_CRC_Error_Count”
)
# Check each SMART attribute
for attr in “${attributes[@]}”; do
attr_line=$(echo “$output” | grep -i “$attr”)
attr_value=$(echo “$attr_line” | awk ‘{print $10}’)
attr_value=${attr_value:-0}
echo “$disk – $attr: $attr_value” >> “$TMPFILE”
if [[ “$attr_value” =~ ^[0-9]+$ ]] && [ “$attr_value” -gt 0 ]; then
alert_triggered=1
fi
done
else
echo “$disk – SMART not supported or accessible” >> “$TMPFILE”
fi
echo “” >> “$TMPFILE”
done
# Determine email subject
if [ “$alert_triggered” -eq 1 ]; then
EMAIL_SUBJECT=”$SUBJECT_ALERT”
else
EMAIL_SUBJECT=”$SUBJECT_OK”
fi
# Send email always, with the right subject
{
echo “To: $TO”
echo “From: $FROM”
echo “Subject: $EMAIL_SUBJECT”
echo
cat “$TMPFILE”
} | $SENDMAIL -t
# Log everything to file
{
echo “[$(date ‘+%Y-%m-%d %H:%M:%S’)] Email sent – Subject: $EMAIL_SUBJECT”
cat “$TMPFILE”
echo “—————————————-“
} >> “$LOGFILE”