Skip to content

Instantly share code, notes, and snippets.

@seniorihor
Created August 14, 2012 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seniorihor/3353114 to your computer and use it in GitHub Desktop.
Save seniorihor/3353114 to your computer and use it in GitHub Desktop.
shutdowner
#!/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