Advertisement
Guest User

spindown.sh

a guest
Sep 14th, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.66 KB | None | 0 0
  1. #! /bin/bash
  2. # Check for idle disks and spin them down
  3. # run this from cron every 30 mins
  4. #     */30 * * * * /usr/local/bin/spindown.sh
  5. #  -- craiger, parts taken from Zack Reed
  6.  
  7. # space separated list of disk wwn names to monitor.
  8. # These should be found in /dev/disk/by-id:
  9. DISKNAMES="wwn-0x5000cca222d23f5f "
  10.  
  11.  
  12.  
  13.  
  14. export PATH=$PATH:/sbin:/usr/sbin
  15.  
  16. # Create a file on the ramdisk and cycle it to test for disk activity
  17. ( if [ ! -f /dev/shm/1 ] ; then touch /dev/shm/1 /dev/shm/2; fi ; mv /dev/shm/1 /dev/shm/2; cat /proc/diskstats > /dev/shm/1 ) >/dev/null 2>&1
  18.  
  19. date
  20.  
  21. # convert disk wwn id's into sdX names because kernel developers
  22. # think they're so clever and randomize sd* names at boot, while most
  23. # userspace tools assume they're static, including /proc/diskstats
  24. for id in $DISKNAMES
  25. do
  26.   SDLIST="$SDLIST `readlink /dev/disk/by-id/$id | cut -d/ -f3`"
  27. done
  28.  
  29. # Loop through all disks and spin them down if idle.
  30. for disk in $SDLIST
  31. do
  32.   # Check if disk exists
  33.   if [ -e /dev/$disk ]; then
  34.      # Check if disk is currently spinning
  35.      if [ "$(hdparm -C /dev/$disk | grep state)" = " drive state is:  active/idle" ]; then
  36.          #Check if disk has been accessed since last time
  37.          if [ "$(diff /dev/shm/1 /dev/shm/2 | grep $disk )" =  "" ]; then                        
  38.             echo "Drive $disk is spun up and idle.  Spinning down now."
  39.             hdparm -y /dev/$disk
  40.          else
  41.             echo "Drive $disk is spun up and being accessed.  No change."
  42.          fi
  43.      else
  44.         echo "Drive $disk is already spun down.  No change."
  45.      fi
  46.   else
  47.      echo "Drive $disk not found."   #will never happen
  48.   fi
  49. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement