fix god binary daemonization, create some sample setups
[god.git] / bin / god
blob2603a48163bf34aa2e9b0c6c0fb205f6f39366f4
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
49 end.parse!
51 if options[:version]
52 require 'god'
54 # print version
55 puts "Version #{God::VERSION}"
56 exit!
57 elsif command = ARGV[0]
58 require 'god'
60 # a command was specified
62 # disable at_exit
63 # module God; def self.at_exit; end; end
65 # get the name of the watch/group
66 name = ARGV[1]
68 # connect to remote drb
69 DRb.start_service
70 server = DRbObject.new nil, "druby://localhost:#{options[:port]}"
72 begin
73 puts "Sending '#{command}' command"
75 # send command
76 watches = server.control(name, command)
78 # output response
79 puts 'The following watches were affected:'
80 watches.each do |w|
81 puts ' ' + w.name
82 end
83 rescue God::InvalidCommandError
84 abort "Command '#{command}' is not valid. Run 'god --help' for usage"
85 end
87 exit!
88 else
89 # start god
90 if !options[:daemonize]
91 require 'god'
92 load File.expand_path(options[:config])
93 else
94 pid = fork do
95 begin
96 require 'god'
98 # Dir.chdir "/"
100 log_file = options[:log] || "/dev/null"
102 STDIN.reopen "/dev/null"
103 STDOUT.reopen(log_file, "a")
104 STDERR.reopen STDOUT
106 puts "Starting god"
108 unless God::EventHandler.loaded?
109 puts
110 puts "***********************************************************************"
111 puts "*"
112 puts "* Event conditions are not available for your installation of god."
113 puts "* You may still use and write custom conditions using the poll system"
114 puts "*"
115 puts "***********************************************************************"
116 puts
119 puts "Resetting file descriptors"
121 # Daemons.daemonize if options[:daemonize]
123 puts "Loading config"
125 load File.expand_path(options[:config])
126 rescue => e
127 File.open('god.log', 'a') { |f| f.puts e.message + "\n" + e.backtrace }
128 abort "!!! ERROR !!!"
132 pid_file = options[:pid] || "god.pid"
133 File.open(pid_file, 'w') { |f| f.write pid }
135 ::Process.detach pid
137 exit!