Skip to content

Instantly share code, notes, and snippets.

@aont
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save aont/71c37fcd32815f2e94e6 to your computer and use it in GitHub Desktop.

Select an option

Save aont/71c37fcd32815f2e94e6 to your computer and use it in GitHub Desktop.
auto shutdown when low battery
#!/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