Skip to content

Instantly share code, notes, and snippets.

@tylerjohnst
Last active February 12, 2019 17:26
Show Gist options
  • Save tylerjohnst/e90daa4038fb14333d3046adc091df6d to your computer and use it in GitHub Desktop.
Save tylerjohnst/e90daa4038fb14333d3046adc091df6d to your computer and use it in GitHub Desktop.
Ability have the shell wait until a script returns a non zero
#!/usr/bin/env ruby
require 'optparse'
options = {
retries: 10,
delay: 1
}
parser = OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.on("-c", "--command THING", "Required: Command to execute") do |command|
options[:command] = command
end
opts.on("-r", "--retry COUNT", Integer, "Number of retries to perform (default: #{options[:retries]})") do |retries|
options[:retries] = retries
end
opts.on("-d", "--delay COUNT", Integer, "Seconds to wait for each check (default: #{options[:delay]})") do |delay|
options[:delay] = delay
end
end
parser.parse!
tries = 0
successful = false
command, retries, delay = options.values_at(:command, :retries, :delay)
if command.nil?
puts parser
exit 1
end
while tries < retries && successful == false
system command
if $?.success?
successful = true
else
tries += 1
puts "Failed (#{tries} of #{retries})"
sleep delay
end
end
if successful
puts "Success! Executing next script."
exit 0
else
puts "Failed to run '#{command}' command after #{tries} attempts."
exit $?.exitstatus
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment