mirror of
https://codeberg.org/privacy1st/nix-git
synced 2024-12-23 02:36:05 +01:00
add script to monitor disk power state
This commit is contained in:
parent
b2e0d29801
commit
eea34a5b5c
44
examples/disk-state.sh
Normal file
44
examples/disk-state.sh
Normal file
@ -0,0 +1,44 @@
|
||||
#!/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
|
Loading…
Reference in New Issue
Block a user