Skip to content

Instantly share code, notes, and snippets.

@tatzyr
Created March 20, 2017 12:07
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 tatzyr/a1f4cc29980f0eb65a9e73e8b8fa8c57 to your computer and use it in GitHub Desktop.
Save tatzyr/a1f4cc29980f0eb65a9e73e8b8fa8c57 to your computer and use it in GitHub Desktop.
loadaverage sleep
#!/usr/bin/env ruby
require "optparse"
opts = ARGV.getopts("", "minutes:5", "load:", "help", "version")
if opts["help"]
puts <<EOS
loadsleep
Usage:
loadsleep [options] SCREEN_NAME
Options:
-m, --minutes=<minutes> System load average for the past x minutes [default: 5]
-l, --load=<load> Load average
-v, --version Print version and exit
-h, --help Show this message
EOS
exit
end
if opts["version"]
puts "loadsleep 0.1.0"
exit
end
if opts["load"].nil?
warn "Error: option '--load' needs a parameter."
exit
end
input = opts["load"].to_f
minutes = opts["minutes"].to_i
case minutes
when 1, 5, 15
else
warn "Error: option '--minutes' parameter must be 1, 5, or 15"
exit
end
loop do
loadaverages = `uptime`.match(/load average: (.+?), (.+?), (.+?)\Z/).captures.map(&:to_f)
loadaverage_h = [[1, 5, 15], loadaverages].transpose.to_h
break if loadaverage_h[minutes] < input
sleep 5
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment