|
本帖最后由 elo1111 于 2020-2-29 13:15 编辑
我用这个 script, 每十分钟跑一下, 支持休眠。
hdspindown.sh
- #!/bin/bash
- # This script looks for recent disk access, and if nothing has changed, puts /dev/"drive" into spindown mode.
- # This should be used only is the hdparm power management function is not working.
- # Call this script with cron or manually as desired
- #
- #https://superuser.com/questions/651880/force-spin-down-of-external-hard-drive-on-linux-raspberry-pi
- #
- readonly SCRIPT_NAME=$(basename $0)
- # Change which drive this script looks at by changing the drive variable below:
- drive="sda"
- #
- isMounted () { findmnt -rno SOURCE,TARGET "$1" >/dev/null;} #path or device
- isDevMounted () { findmnt -rno SOURCE "$1" >/dev/null;} #device only
- isPathMounted() { findmnt -rno TARGET "$1" >/dev/null;} #path only
- #
- log() {
- echo "$@"
- logger -p user.notice -t $SCRIPT_NAME "$@"
- }
- #
- if isDevMounted "/dev/$drive1"; then
- echo "device is mounted"
- else
- log "device is not mounted"
- reboot
- fi
- if isMounted "/dev/disk/by-label/*****Data*****"; then
- echo "disk is mounted"
- else
- log "disk is not mounted"
- # reboot
- fi
- #
- if (hdparm -C /dev/$drive |grep -q "standby") then
- log "Disk is standing by"
- exit 0
- fi
- if (netstat -ano|grep 8200|grep -q "ESTABLISHED") then
- log "MiniDLNA is in-use"
- exit 0
- fi
- if (netstat -ano|grep 139|grep -q "ESTABLISHED") then
- log "Samba is in-use"
- exit 0
- fi
- #
- if (netstat -ano|grep -q "ESTABLISHED") then
- log "Some ports are in-used"
- (netstat -ano|grep "ESTABLISHED") 2>&1 | (log)
- fi
- #
- if [ $(smartctl -a /dev/$drive | grep -c "Self-test routine in progress") = 1 ]; then
- log "Smart test in progress"
- exit 0
- fi
- #
- current=`date`
- caller=$(ps ax | grep "^ *$PPID" | awk '{print $NF}')
- filename="/tmp/diskaccess.txt"
- if [ -f "$filename" ]; then
- stat_old=`cat "$filename" | tr -dc "[:digit:]"`
- stat_new=`cat /sys/block/"$drive"/stat | tr -dc "[:digit:]"`
- if [ "$stat_old" == "$stat_new" ]; then
- stat="0"
- log "The disk hasn't been used; spinning down /dev/$drive"
- log $stat_old
- hdparm -y /dev/$drive > /dev/null
- else
- stat="1"
- log $stat_old
- log $stat_new
- log "The drive has been used..."
- echo $stat_new > $filename
- fi
- else
- log "/tmp/diskaccess.txt file does not exist; creating it now."
- log $stat_new > $filename
- fi
- echo $stat " - " $drive " - " $current " - by: " $caller >> /tmp/diskaccesslog.txt
复制代码
|
|