module OpsScript
This module provides some basic features common to many operational unix scripts
-
Log file
-
PID file used to insure only one instance is running, handy if running from cron multiple times
-
Options processing with basic options for overriding default log attributes and logfile and pidfile location
Example¶ ↑
require 'ops_script' include OpsScript # Add your own custom options option_parser.on "--bar", "The bar option" do options[:bar] = true end # Run your code OpsScript.run do puts "BAR" if options[:bar] log.info "My informational message" log.debug "This is a debug message" # do some stuff end
The module provides these options
foo.rb -h Usage: foo.rb [options] --debug enable debug output --logfile log_file specify a different log file default is ../log/foo.log --log-retention retention Number of old log files to keep, or frequency of rotation (daily, weekly or monthly). Defaults to monthly. --log-size size Max size in bytes for logfile before rotating. Specify number of files to keep with log-retention option. --pidfile pid_file specify a different pid file default is ../tmp/foo.pid -h, -?, --help Display this help
Constants
- VERSION
Public Class Methods
log()
click to toggle source
# File lib/ops_script.rb, line 135 def self.log @log ||= Logger.new(options[:log_file], options[:log_retention] || 'monthly', options[:log_size]) end
option_parser()
click to toggle source
# File lib/ops_script.rb, line 65 def self.option_parser @option_parser ||= OptionParser.new end
options()
click to toggle source
# File lib/ops_script.rb, line 57 def self.options @options ||= {} end
run() { || ... }
click to toggle source
# File lib/ops_script.rb, line 144 def self.run process_options if already_running? puts "Found #{options[:pid_file]} file. Must be running already. Exiting." exit else create_directories create_pid log.level = Logger::INFO unless options[:debug] log.info "--" log.info "#{script} starting" end begin if block_given? yield else raise "No block given. Nothing to run." end rescue => e log.error e.message log.debug e.backtrace.join('\n') raise ensure log.info "#{script} exiting" log.close remove_pid end end
script_basename()
click to toggle source
# File lib/ops_script.rb, line 48 def script_basename File.basename $0, '.*' end
Public Instance Methods
log()
click to toggle source
Use log in your script to log messages
# File lib/ops_script.rb, line 131 def log OpsScript.log end
option_parser()
click to toggle source
# File lib/ops_script.rb, line 61 def option_parser OpsScript.option_parser end
options()
click to toggle source
# File lib/ops_script.rb, line 53 def options OpsScript.options end
Private Instance Methods
already_running?()
click to toggle source
# File lib/ops_script.rb, line 115 def already_running? File.exists?(options[:pid_file]) end
create_directories()
click to toggle source
# File lib/ops_script.rb, line 109 def create_directories FileUtils.mkdir_p File.dirname(File.expand_path(options[:log_file])) FileUtils.mkdir_p File.dirname(File.expand_path(options[:pid_file])) end
create_pid()
click to toggle source
# File lib/ops_script.rb, line 120 def create_pid ::File.open(options[:pid_file], 'w' ){ |f| f.write("#{Process.pid}")} end
process_options()
click to toggle source
# File lib/ops_script.rb, line 69 def process_options options[:log_file] = File.expand_path("../../log/#{script_basename}.log", $0) options[:pid_file] = File.expand_path("../../tmp/#{script_basename}.pid", $0) option_parser.on_tail("-h","-?","--help", "Display this help") do puts option_parser exit end option_parser.on("--debug", "enable debug output") do options[:debug] = true end option_parser.on("--logfile log_file", "specify a different log file", "default is #{options[:log_file]}") do |p| options[:log_file] = p end option_parser.on("--log-retention retention", "Number of old log files to keep,", "or frequency of rotation (daily, weekly or monthly). Defaults to monthly.") do |r| options[:log_retention] = r end option_parser.on("--log-size size", "Max size in bytes for logfile before rotating.", "Specify number of files to keep with log-retention option.") do |s| options[:log_size] = s end option_parser.on_tail("-h","-?","--help", "Display this help") do puts option_parser exit end option_parser.on("--pidfile pid_file", "specify a different pid file", "default is #{options[:pid_file]}") do |p| options[:pid_file] = p end option_parser.parse! end
remove_pid()
click to toggle source
# File lib/ops_script.rb, line 125 def remove_pid File.unlink(options[:pid_file]) end
script()
click to toggle source
# File lib/ops_script.rb, line 139 def script File.basename $0 end
script_basename()
click to toggle source
# File lib/ops_script.rb, line 48 def script_basename File.basename $0, '.*' end