Advertisement
Guest User

Untitled

a guest
Jan 5th, 2016
1,297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.87 KB | None | 0 0
  1. #!/bin/bash
  2. # Created by marco, Di 5. Jan 20:11:25 CET 2016.
  3.  
  4. GOVERNOR_NOCHARGE=powersave # CPU governor when not charging
  5. GOVERNOR_CHARGE=performance # CPU governor when charging
  6. recur=60 # Set governor after x seconds just in case it didn't correctly set or get set by another program; comment "let ticks--" line in function main to disable
  7.  
  8. # Don't change values below this text
  9. ticks=$recur
  10. laststate=1337
  11. state=
  12.  
  13. CPUPOWER=cpupower
  14. ACPI=acpi
  15.  
  16. function die {
  17.   echo "$@"
  18.   exit 1
  19. }
  20.  
  21. function checkACPluggedIn {
  22.   st="$($ACPI -a | cut -d' ' -f3)"
  23.   if [ "x$st" = "xoff-line" ]; then
  24.     echo "0"
  25.   elif [ "x$st" = "xon-line" ]; then
  26.     echo "1"
  27.   else
  28.     echo "-1"
  29.   fi
  30. }
  31.  
  32. function onSwitchState {
  33.   echo
  34.   ticks=$recur
  35.   if [ $state -eq 0 ]; then
  36.     $CPUPOWER frequency-set -g $GOVERNOR_NOCHARGE
  37.     echo "Switched to governor $GOVERNOR_NOCHARGE"
  38.   fi
  39.   if [ $state -eq 1 ]; then
  40.     $CPUPOWER frequency-set -g $GOVERNOR_CHARGE
  41.     echo "Switched to governor $GOVERNOR_CHARGE"
  42.   fi
  43. }
  44.  
  45. function main {
  46.   while true; do
  47.     state="$(checkACPluggedIn)"
  48.     if [ $state -lt 0 ]; then
  49.       die "An unexpected error occurred. Does this computer have a battery?"
  50.     fi
  51.     if [ ! $state -eq $laststate ]; then
  52.       onSwitchState
  53.     fi
  54.     laststate="$state"
  55.     let ticks--
  56.     if [ $ticks -lt 1 ]; then
  57.       onSwitchState
  58.     fi
  59.     sleep 1
  60.   done
  61. }
  62.  
  63. if [ ! $UID -eq 0 ]; then
  64.   die "This script must be run as root."
  65. fi
  66.  
  67. noexec=0
  68.  
  69. echo -n "cpupower: "
  70. CPUPOWER="$(which cpupower 2>/dev/null)"
  71. if [ $? -eq 0 ]; then
  72.   echo $CPUPOWER
  73. else
  74.   echo not found
  75.   noexec=1
  76. fi
  77.  
  78. echo -n "acpi: "
  79. ACPI="$(which acpi 2>/dev/null)"
  80. if [ $? -eq 0 ]; then
  81.   echo $ACPI
  82. else
  83.   echo not found
  84.   noexec=1
  85. fi
  86.  
  87. if [ ! $noexec -eq 0 ]; then
  88.   die "One or more required programs have not been found. Please install them."
  89. fi
  90.  
  91. main
  92. exit $?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement