/
auto-idle-shutdown.rb
33 lines (26 loc) · 983 Bytes
/
auto-idle-shutdown.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
require "yaml"
#sleep(5)
DEBUG = ARGV.include?("--debug")
def debug(msg)
puts msg if DEBUG
end
if !File.exists?("auto-idle-shutdown.conf")
puts "Missing configuration file."
exit!(1)
end
config = YAML::load( File.open("auto-idle-shutdown.conf") )
@shutdown = config["shutdown"].to_i # Already in minutes
debug "Status: Shutdown after #{@shutdown} minutes"
@notification = config["notification"].to_i # Already in minutes
debug "Status: Notify #{@notification} minutes before shutdown."
# ./idle returns in milliseconds.
@idle = `./idle`.to_i / 60000 # there's `60 * 1000 ms` in a minute
debug "Status: Idling for #{@idle}"
if @idle >= @shutdown
debug "Shutting down."
elsif @idle >= (@shutdown - @notification)
debug "Alerting user that PC will shut down in approximately #{@notification}."
`zenity --warning --text="AUTO-IDLE-SHUTDOWN: Your PC will shut down in approximately 10 minutes if you don't touch your mouse or keyboard."`
else
debug "Do nothing."
end