Created
August 14, 2012 21:18
-
-
Save seniorihor/3353114 to your computer and use it in GitHub Desktop.
shutdowner
This file contains 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/env ruby | |
# encoding: utf-8 | |
class Shutdown | |
def initialize(*options) | |
@action = options[0] | |
@time = options[1] | |
@allow_actions = %w(halt reboot) | |
end | |
def print(format, time) | |
puts "#{time} #{format}(s) to the end..." | |
end | |
def finish | |
puts 'TIME OUT!!!' | |
`sudo shutdown -#{@action[0]} now` | |
end | |
def run | |
exit unless @allow_actions.include?(@action) | |
self.finish if @time == 0 | |
@time.downto(1) do |time| | |
if time == 1 | |
60.downto(1) { |second| self.print('second', second); sleep 1 } | |
self.finish | |
else | |
`sudo echo` | |
self.print('minute', time) | |
sleep 60 | |
end | |
end | |
end | |
end | |
puts 'Halt or reboot?' | |
action = gets.chomp | |
puts 'Enter time:' | |
time = gets.chomp.to_i | |
puts 'TIPS: You must be superuser!' | |
`sudo echo` | |
exit if action.empty? || time.nil? | |
Shutdown.new(action, time).run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment