fix byte-centric sugar (kb is canonical not bytes); make god binary pid generation...
[god.git] / bin / god
blob9036a616094bd1f90e9ff22a681ce76b5451a6c8
1 #!/usr/bin/env ruby
3 $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
5 require 'rubygems'
6 require 'optparse'
7 require 'drb'
9 require 'daemons'
11 options = {:daemonize => true, :port => 17165}
13 OptionParser.new do |opts|
14 opts.banner = <<-EOF
15 Usage: god [command] [options]
17 Commands:
18 start <watch or group name>
19 stop <watch or group name>
20 monitor <watch or group name>
21 unmonitor <watch or group name>
23 Options:
24 EOF
26 opts.on("-cCONFIG", "--config-file CONFIG", "Configuration file") do |x|
27 options[:config] = x
28 end
30 opts.on("-pPORT", "--port PORT", "Communications port") do |x|
31 options[:port] = x
32 end
34 opts.on("-PFILE", "--pid FILE", "Where to write the PID file") do |x|
35 options[:pid] = x
36 end
38 opts.on("-lFILE", "--log FILE", "Where to write the log file") do |x|
39 options[:log] = x
40 end
42 opts.on("-D", "--no-daemonize", "Don't daemonize") do
43 options[:daemonize] = false
44 end
46 opts.on("-v", "--version", "Print the version number and exit") do
47 options[:version] = true
48 end
50 opts.on("-V", "Print extended version and build information") do
51 options[:info] = true
52 end
53 end.parse!
55 if options[:version]
56 require 'god'
58 # print version
59 puts "Version #{God::VERSION}"
60 exit!
61 elsif options[:info]
62 require 'god'
64 puts "Version: #{God::VERSION}"
65 puts "Polls: enabled"
66 puts "Events: " + God::EventHandler.event_system
68 exit!
69 elsif command = ARGV[0]
70 require 'god'
72 # a command was specified
74 # disable at_exit
75 # module God; def self.at_exit; end; end
77 # get the name of the watch/group
78 name = ARGV[1]
80 # connect to remote drb
81 DRb.start_service
82 server = DRbObject.new nil, "druby://localhost:#{options[:port]}"
84 begin
85 puts "Sending '#{command}' command"
87 # send command
88 watches = server.control(name, command)
90 # output response
91 puts 'The following watches were affected:'
92 watches.each do |w|
93 puts ' ' + w.name
94 end
95 rescue God::InvalidCommandError
96 abort "Command '#{command}' is not valid. Run 'god --help' for usage"
97 end
99 exit!
100 else
101 # start god
102 if !options[:daemonize]
103 require 'god'
104 load File.expand_path(options[:config])
105 else
106 pid = fork do
107 begin
108 require 'god'
110 log_file = options[:log] || "/dev/null"
112 STDIN.reopen "/dev/null"
113 STDOUT.reopen(log_file, "a")
114 STDERR.reopen STDOUT
116 puts "Starting god"
118 unless God::EventHandler.loaded?
119 puts
120 puts "***********************************************************************"
121 puts "*"
122 puts "* Event conditions are not available for your installation of god."
123 puts "* You may still use and write custom conditions using the poll system"
124 puts "*"
125 puts "***********************************************************************"
126 puts
129 puts "Resetting file descriptors"
131 puts "Loading config"
133 load File.expand_path(options[:config])
134 rescue => e
135 File.open('god.log', 'a') { |f| f.puts e.message + "\n" + e.backtrace }
136 abort "!!! ERROR !!!"
140 if options[:pid]
141 File.open(options[:pid], 'w') { |f| f.write pid }
144 ::Process.detach pid
146 exit!