Last active
August 31, 2020 00:48
-
-
Save jordancrawford/cb9bd2eca76c0f5991577ae19169bc04 to your computer and use it in GitHub Desktop.
Poll hard drive power management status.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Run with
drive_power_status /dev/[device name]e.g.:
drive_power_status /dev/sdaManually force device sleep:
sudo hdparm -y /dev/[device name]Using systemd
Install the unit:
sudo mv drive_power_status@.service /etc/systemd/systemStart the unit:
sudo systemctl start drive_power_status@[device name].servicee.g.:
sudo systemctl start drive_power_status@sda.serviceStart the unit at boot:
sudo systemctl enable drive_power_status@[device name].servicee.g.:
sudo systemctl enable drive_power_status@sda.serviceGet 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]