Difference between revisions of "ACPI action script for battery events"

From ThinkWiki
Jump to: navigation, search
 
m
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
This is an example ACPI action script that makes Power Management adjustments according to if you are on battery or on AC.
 
This is an example ACPI action script that makes Power Management adjustments according to if you are on battery or on AC.
Usually this is saved as <tt>/etc/acpid/actions/battery.sh</tt>.
+
Usually this is saved as {{path|/etc/acpid/actions/battery.sh}}.
  
 
  #!/bin/sh
 
  #!/bin/sh
  # I' using this on FC2 and FC3
+
  # Tried on Gentoo
  # cpu throttling off as FC does thisout-of-the-box
+
  # cpu throttling is in here it is the cpufreq-set lines
 
  # turning swap off is only for those that feel comfortable
 
  # turning swap off is only for those that feel comfortable
 
  # doing something this nasty.
 
  # doing something this nasty.
  #
+
  # comment out the laptop_mode line if you don't have it installed
  # pcfe, 2004-11-15
+
  # pcfe, 2008-10-28
                                                                                                                                     
+
   
# cpu throttling
 
# cat /proc/acpi/processor/CPU0/throttling for more info
 
  ACAD_THR=0
 
BATT_THR=2
 
                                                                                                                                   
 
 
  # spindown time for HD (man hdparm for valid values)
 
  # spindown time for HD (man hdparm for valid values)
 
  # I prefer 2 hours for acad and 2 min for batt
 
  # I prefer 2 hours for acad and 2 min for batt
 
  ACAD_HD=244
 
  ACAD_HD=244
 
  BATT_HD=24
 
  BATT_HD=24
                                                                                                                                   
+
                                                                                                                                   
 
  # Power management level
 
  # Power management level
 
  # 255 (off) on AC
 
  # 255 (off) on AC
Line 28: Line 23:
 
  ACAD_PM=255
 
  ACAD_PM=255
 
  BATT_PM=96
 
  BATT_PM=96
                                                                                                                                   
+
                                                                                                                                   
 
  # ac/battery event handler
 
  # ac/battery event handler
 +
                                                                                                                                   
 +
status=`awk '/^state: / { print $2 }' /proc/acpi/ac_adapter/AC/state`
 
                                                                                                                                      
 
                                                                                                                                      
  status=`awk '/^state: / { print $2 }' /proc/acpi/ac_adapter/AC/state`
+
  if [ "$status" = "off-line" ]
                                                                                                                                     
+
then
case $status in
+
              logger "Running /sbin/laptop_mode start"
        "on-line")
+
              /sbin/laptop_mode start
                logger "Running /sbin/laptop_mode stop"
+
              logger "Setting HD spindown for BATT mode with hdparm -S $BATT_HD /dev/hda."
                /sbin/laptop_mode stop
+
              /sbin/hdparm -S $BATT_HD /dev/hda > /dev/null 2>&1
                logger "Setting HD spindown for AC mode with hdparm -S $ACAD_HD /dev/hda."
+
              logger "Setting HD powersaving for BATT mode with hdparm -B $BATT_PM /dev/hda."
                /sbin/hdparm -S $ACAD_HD /dev/hda > /dev/null 2>&1
+
              /sbin/hdparm -B $BATT_PM /dev/hda > /dev/null 2>&1
                logger "Setting HD powersaving for AC mode with hdparm -B $ACAD_PM /dev/hda."
+
              logger "Turning off swap."
                /sbin/hdparm -B $ACAD_PM /dev/hda > /dev/null 2>&1
+
              /sbin/swapoff -a
                #logger "Turning on swap."
+
      /usr/bin/cpufreq-set -g conservative          
                #/sbin/swapon -a
+
else 
                #echo -n $ACAD_CPU:$ACAD_THR > /proc/acpi/processor/CPU0/limit
+
              logger "Turning on swap."
                exit 0
+
              /sbin/swapon -a
         ;;
+
              logger "Running /sbin/laptop_mode start"
        "off-line")
+
              /sbin/laptop_mode stop
                #logger "Turning off swap."
+
              logger "Setting HD spindown for AC mode with hdparm -S $ACAD_HD /dev/hda."
                #/sbin/swapoff -a
+
              /sbin/hdparm -S $ACAD_HD /dev/hda > /dev/null 2>&1
                logger "Running /sbin/laptop_mode start"
+
              logger "Setting HD powersaving for AC mode with hdparm -B $ACAD_PM /dev/hda."
                /sbin/laptop_mode start
+
              /sbin/hdparm -B $ACAD_PM /dev/hda > /dev/null 2>&1
                logger "Setting HD spindown for battery mode with hdparm -S $BATT_HD /dev/hda."
+
              /usr/bin/cpufreq-set -g ondemand
                /sbin/hdparm -S $BATT_HD /dev/hda > /dev/null 2>&1
+
fi
                logger "Setting HD powersaving for battery mode with hdparm -B $BATT_PM /dev/hda."
+
 
                /sbin/hdparm -B $BATT_PM /dev/hda > /dev/null 2>&1
+
On archlinux you might need to add an event like a file e.g. saved as {{path|/etc/acpi/events/battery_event}} with following lines in it:
                #echo -n $BATT_CPU:$BATT_THR > /proc/acpi/processor/CPU0/limit
+
 
                exit 0
+
event=battery.*
        ;;
+
action=/etc/acpi/actions/battery.sh
esac
+
 
 +
Note also on archlinux the acpi actions and events are located in {{path|/etc/acpi}} (not {{path|/etc/acpid}} as in gentoo)
 +
 
 +
After restarting your acpi daemon the script should be activated on switching the power supply on/off. You could check your logs to be sure that everything works as expected.
  
 
[[Category:Scripts]]
 
[[Category:Scripts]]

Latest revision as of 23:54, 8 January 2009

This is an example ACPI action script that makes Power Management adjustments according to if you are on battery or on AC. Usually this is saved as /etc/acpid/actions/battery.sh.

#!/bin/sh
# Tried on Gentoo
# cpu throttling is in here it is the cpufreq-set lines
# turning swap off is only for those that feel comfortable
# doing something this nasty.
# comment out the laptop_mode line if you don't have it installed
# pcfe, 2008-10-28

# spindown time for HD (man hdparm for valid values)
# I prefer 2 hours for acad and 2 min for batt
ACAD_HD=244
BATT_HD=24
                                                                                                                                   
# Power management level
# 255 (off) on AC
# 128 (medium) on batt
# lowered to 32, pcfe, 2004-06-23
# upped to 64, pcfe, 2004-07-14
# upped to 96, pcfe, 2004-10-20
ACAD_PM=255
BATT_PM=96
                                                                                                                                   
# ac/battery event handler
                                                                                                                                   
status=`awk '/^state: / { print $2 }' /proc/acpi/ac_adapter/AC/state`
                                                                                                                                    
if [ "$status" = "off-line" ]
then
              logger "Running /sbin/laptop_mode start"
              /sbin/laptop_mode start
              logger "Setting HD spindown for BATT mode with hdparm -S $BATT_HD /dev/hda."
              /sbin/hdparm -S $BATT_HD /dev/hda > /dev/null 2>&1
              logger "Setting HD powersaving for BATT mode with hdparm -B $BATT_PM /dev/hda."
              /sbin/hdparm -B $BATT_PM /dev/hda > /dev/null 2>&1
              logger "Turning off swap."
              /sbin/swapoff -a
	       /usr/bin/cpufreq-set -g conservative         
else   
              logger "Turning on swap."
              /sbin/swapon -a
              logger "Running /sbin/laptop_mode start"
              /sbin/laptop_mode stop
              logger "Setting HD spindown for AC mode with hdparm -S $ACAD_HD /dev/hda."
              /sbin/hdparm -S $ACAD_HD /dev/hda > /dev/null 2>&1
              logger "Setting HD powersaving for AC mode with hdparm -B $ACAD_PM /dev/hda."
              /sbin/hdparm -B $ACAD_PM /dev/hda > /dev/null 2>&1
              /usr/bin/cpufreq-set -g ondemand
fi

On archlinux you might need to add an event like a file e.g. saved as /etc/acpi/events/battery_event with following lines in it:

event=battery.*
action=/etc/acpi/actions/battery.sh 

Note also on archlinux the acpi actions and events are located in /etc/acpi (not /etc/acpid as in gentoo)

After restarting your acpi daemon the script should be activated on switching the power supply on/off. You could check your logs to be sure that everything works as expected.