Create a gist now

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
$KCODE='utf8'
require 'time'
require 'logger'
def usage(error=nil)
STDERR.print <<-EOS
sleep until the wakeup time.
[usage] sleep wakeuptime
-d
-v
EOS
STDERR.print "error:#{error}\n" if (error)
end
$log = Logger.new(STDERR)
$log.level = Logger::WARN
def debug(msg)
$log.debug(msg)
end
def info(msg)
$log.info(msg)
end
def error(msg)
$log.error(msg)
exit
end
if (!ARGV[0])
usage()
exit
end
if (ARGV.index('-d'))
$log.level = Logger::DEBUG
end
now = Time.now
wakeup = Time.parse(ARGV[0])
wakeup = wakeup+(60*60*24) if (wakeup<now)
if (wakeup<now)
error("suspicious wakeup time:#{ARGV[0]}")
end
sleeptime = wakeup-now
debug("now:#{now.to_s}")
debug("wakeup at:#{wakeup.to_s}")
debug("sleep:#{sleeptime}")
sleep(sleeptime)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment