Skip to content

Instantly share code, notes, and snippets.

@jordancrawfordnz
Last active August 31, 2020 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordancrawfordnz/cb9bd2eca76c0f5991577ae19169bc04 to your computer and use it in GitHub Desktop.
Save jordancrawfordnz/cb9bd2eca76c0f5991577ae19169bc04 to your computer and use it in GitHub Desktop.
Poll hard drive power management status.
#!/bin/bash
if [ $# != 1 ]
then
echo "drive_power_status [name of device to watch]"
exit
fi
device=$1
echo "Started watching $device state"
last_status="unknown"
while true
do
current_status=`hdparm -C $device | grep -o 'unknown\|\active\|idle\|active/idle\|standby\|sleeping'`
if [ "$last_status" != "$current_status" ]
then
echo "`date` - $device status - $current_status"
fi
last_status=$current_status
sleep 10
done
[Unit]
Description=job polling the hard drive power status for /dev/%i
[Service]
Type=simple
ExecStart=/bin/bash /usr/bin/drive_power_status /dev/%i
[Install]
WantedBy=multi-user.target
@jordancrawfordnz
Copy link
Author

jordancrawfordnz commented Aug 28, 2020

Systemd setup instructions were based on a guide for basic systemd units: https://www.linode.com/docs/quick-answers/linux/start-service-at-boot/

Requires a system with systemd and hdparm.

Installing

sudo chmod +x drive_power_status
sudo mv drive_power_status /usr/bin/drive_power_status

Run with drive_power_status /dev/[device name]

e.g.: drive_power_status /dev/sda

Manually force device sleep:
sudo hdparm -y /dev/[device name]

Using systemd
Install the unit:
sudo mv drive_power_status@.service /etc/systemd/system

Start the unit:
sudo systemctl start drive_power_status@[device name].service
e.g.: sudo systemctl start drive_power_status@sda.service

Start the unit at boot:
sudo systemctl enable drive_power_status@[device name].service
e.g.: sudo systemctl enable drive_power_status@sda.service

Get unit status with:
sudo systemctl status drive_power_status@[device name].service

Use journalctl to see the status. Follow with sudo journalctl -u drive_power_status@[device name]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment