Skip to content

Instantly share code, notes, and snippets.

@kmdsbng
Created January 21, 2010 04:08
Show Gist options
  • Save kmdsbng/282568 to your computer and use it in GitHub Desktop.
Save kmdsbng/282568 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# ruby timer.rb 3m ramen_ok &
require 'time'
require 'date'
NOHUP_FLAG = '___nohup___'
def main(delay, message)
wait(parse_time(delay))
notify(message)
end
def wait(sec)
puts "sleep #{sec}sec"
sleep sec
end
def parse_time(time)
return 0 if time.to_i == 0
if time =~ /^([0-9]+)([a-zA-Z]*)$/
$1.to_i * rate($2)
elsif time =~ /^([0-9]+):([0-9]+)$/
next_time = get_next_time($1, $2)
next_time.to_i - Time.now.to_i
else
time.to_i
end
end
def get_next_time(hour, min)
now = Time.now
t = make_time(Date.today, hour, min)
(t > now) ? t : make_time(Date.today + 1, hour, min)
end
def make_time(date, hour, min)
Time.parse(date.strftime('%Y/%m/%d ') + ("%02d:%02d" % [hour, min]))
end
def rate(unit)
case (unit)
when /s(ec)?/i
1
when /m(in)?/i
60
when /h(our)?/i
60 * 60
else
1
end
end
def notify(message)
msg = message || "notice from #{hostname}"
system('notify_send', message)
end
if (ARGV[0] != NOHUP_FLAG)
#system(*(['nohup', 'ruby', $0, NOHUP_FLAG] + ARGV))
%x(#{(['nohup', 'ruby', __FILE__, NOHUP_FLAG] + ARGV.map{|s| '"' + s + '"'}).join(' ')})
else
main(ARGV[1], ARGV[2])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment