powersave
#! /bin/sh
### BEGIN INIT INFO
# Provides:          powersave
# Required-Start:    checkroot
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:	     0 1 6
# Short-Description: Set server powersave options.
# Description:       Set powersave options for server low-power usage.
### END INIT INFO
 
###########################################################################
##  Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3      ##
##  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  ##
##  rights of fair usage, the disclaimer and warranty conditions.        ##
###########################################################################
 
#
# This script is based off: http://crunchbanglinux.org/forums/topic/11954
# and turned into an init-level script for Debian. It sets a few options 
# that will make your server consume less power. The idea stems from sparing 
# battery power, but the same can be applied to desktop servers since most 
# of the time a lot of hardware (such as USB ports) lay dormant. This benefits 
# hardware in many ways: wear-and-tear, heat, hardware burn-in, etc..
#
 
PATH=/sbin:/bin
 
do_start () {
	# Turn hardware kernel debug off
	if [ -f /proc/sys/kernel/nmi_watchdog ]; then
		echo 0 >/proc/sys/kernel/nmi_watchdog
	fi 
	# Enable Audio codec power management
	if [ -f /sys/module/snd_hda_intel/parameters/power_save_controller ]; then
		echo Y > /sys/module/snd_hda_intel/parameters/power_save_controller
	fi
	if [ -f /sys/module/snd_hda_intel/parameters/power_save ]; then
		echo 1 > /sys/module/snd_hda_intel/parameters/power_save
	fi 
        # USB powersaving
        for i in /sys/bus/usb/devices/*/power/autosuspend; do
          if [ -f $i ]; then
            echo 1 > $i
          fi
        done
        # Enable runtime power management. Suggested by powertop.
        for bus in pci i2c; do
          for i in /sys/bus/$bus/devices/*/power/control; do
            if [ -f $i ]; then
              echo auto > $i
            fi 
          done
        done
}
 
case "$1" in
  start|"")
	do_start
	;;
  restart|reload|force-reload)
	echo "Error: argument '$1' not supported" >&2
	exit 3
	;;
  stop)
	# No-op
	;;
  *)
	echo "Usage: powersave [start|stop]" >&2
	exit 3
	;;
esac
 
exit 0