Last active
August 29, 2015 14:07
-
-
Save aont/71c37fcd32815f2e94e6 to your computer and use it in GitHub Desktop.
auto shutdown when low battery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/ruby | |
| IO.popen("sudo -Hs", "r+") do |sudo| | |
| sudo.puts "echo login_test" | |
| testmes = sudo.gets | |
| if testmes != "login_test\n" | |
| break | |
| end | |
| while true | |
| percent = nil | |
| IO.popen("upower -i /org/freedesktop/UPower/devices/battery_BAT0", "r") do |io| | |
| while line = io.gets | |
| if line =~ /percentage:\s+([^%]+)%/ | |
| percent = $1.to_f | |
| end | |
| end | |
| $stderr.puts percent | |
| end | |
| if percent < 30 | |
| puts "-------- going to shutdown ---------" | |
| sudo.puts 'shutdown -h now' | |
| break | |
| end | |
| sleep 10 | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment