#!/usr/bin/env bash # # Watch disk power state (HDD spin-up and spin-down events). # # Arguments. disk='/dev/disk/by-id/ata-WDC_WD40EFRX-68N32N0_WD-WCC7K0CPF0N1' # Assert that this script is run as root. if [ "$(id -u)" -ne 0 ]; then printf '%s\n' 'Error: This script has to be run as root user.' 1>&2 exit 1 fi trap ctrl_c INT function ctrl_c() { echo "End: $(date +"%Y-%m-%dT%H:%M:%S%z")" exit 0 } function notify-spin-down(){ while hdparm -C "${disk}" | grep 'active/idle' >/dev/null; do sleep 15s done echo "$(date +"%Y-%m-%dT%H:%M:%S%z"): ${disk} has spun down" } function notify-spin-up(){ while hdparm -C "${disk}" | grep 'standby' >/dev/null; do sleep 15s done echo "$(date +"%Y-%m-%dT%H:%M:%S%z"): ${disk} has spun up" } function main(){ echo "Start: $(date +"%Y-%m-%dT%H:%M:%S%z")" hdparm -C "${disk}" | grep 'active/idle' >/dev/null && notify-spin-down hdparm -C "${disk}" | grep 'standby' >/dev/null && { while :; do notify-spin-up notify-spin-down done } echo "${disk} has unknown state" } main