bin/cognizantd
#!/usr/bin/env ruby
# Set the process name.
$0 = File.basename(__FILE__)
# Flush standard output/error immediately.
$stdout.sync = true
$stderr.sync = true
begin
require "optparse"
require "cognizant/version"
require "cognizant/controller"
require "cognizant/util/transform_hash_keys"
options = {}
OptionParser.new do |opts|
opts.banner = <<-EOF
NAME
cognizantd - system utility daemon to supervise your processes
SYNOPSIS:
cognizantd [global options] [config file | -]
VERSION
#{Cognizant::VERSION}
GLOBAL OPTIONS:
EOF
opts.on("--[no-]daemonize", "Whether or not to daemonize cognizantd into background.") do |value|
options[:daemonize] = value
end
opts.on("--sockfile FILE", String, "The socket lock file of the command server.") do |value|
options[:pidfile] = value
end
opts.on("--pidfile FILE", String, "The pid (process identifier) lock file for the daemon.") do |value|
options[:pidfile] = value
end
opts.on("--syslog", String, "Log the daemon's operational information into syslog.") do |_|
options[:syslog] = true
end
opts.on("--logfile FILE", String, "Log the daemon's operational information into a file.") do |value|
options[:logfile] = value
end
opts.on("--loglevel LEVEL", String, "The level of information to log.") do |value|
options[:loglevel] = value
end
opts.on("-t", "--trace", "Turn on tracing, enable full backtrace.") do
options[:trace] = true
end
opts.on("-v", "--version", "Print the version number and exit.") do
require "cognizant/version"
$stdout.puts Cognizant::VERSION
exit(0)
end
end.parse!
config = {}
config_file = ARGV.shift
if config_file == "-"
config = YAML.load(ARGF.read)
elsif config_file.to_s.size > 0
if File.exists?(config_file)
config = YAML.load_file(config_file)
elsif File.exists?(default_config_file = "/etc/cognizant/cognizantd.yml")
config = YAML.load_file(default_config_file)
end
end
config.deep_symbolize_keys!
config = config.merge(options)
trace = config[:trace]
Cognizant::Controller.start_daemon(config)
rescue => exception
if exception.instance_of?(SystemExit)
raise
else
$stderr.puts "ERROR While executing #{$0} ... (#{exception.class})"
$stderr.puts " #{exception.message}\n\n"
if trace
$stderr.puts exception.backtrace.join("\n")
$stderr.puts "\n(See usage by running #{$0} with --help)"
else
$stderr.puts "(See full trace by running #{$0} with --trace)"
end
exit(1)
end
end