> 2. 好似冇 S記的 setting: 硬碟 idle 30min 就自動關機.
AI 教寫下面的 code, 可在硬盤休眠30min 後就自動關機, 大致都 work, 但要再優化下.
---------------------------------------------------------------------------------
#!/bin/bash
HDD_DEVICE="/dev/sda" # Replace with your actual hard disk device
HIBERNATION_TIME=1800 # 30 minutes in seconds
LOG_FILE="/var/log/disk_hibernation_shutdown.log"
while true; do
if ! hdparm -C "$HDD_DEVICE" | grep -q "standby"; then
echo "$(date): HDD is active, resetting timer." >> "$LOG_FILE"
touch /tmp/hdd_active
else
if [[ -f /tmp/hdd_active ]]; then
LAST_MODIFIED=$(stat -c %Y /tmp/hdd_active)
CURRENT_TIME=$(date +%s)
ELAPSED_TIME=$((CURRENT_TIME - LAST_MODIFIED))
if [[ $ELAPSED_TIME -ge $HIBERNATION_TIME ]]; then
echo "$(date): HDD has been in standby for 30 minutes, shutting down..." >> "$LOG_FILE"
rm /tmp/hdd_active
shutdown -h now
fi
else
echo "$(date): HDD entered standby, starting timer." >> "$LOG_FILE"
touch /tmp/hdd_active
fi
fi
sleep 60 # Check every 60 seconds
done |