fix broken daemon code in god binary (sort of)
[god.git] / bin / god
blob727f9b353cecfc743431376270358af87849f48d
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'
10 require 'god'
12 options = {:daemonize => true, :port => 17165}
14 OptionParser.new do |opts|
15 opts.banner = <<-EOF
16 Usage: god [command] [options]
18 Commands:
19 start <watch or group name>
20 stop <watch or group name>
21 monitor <watch or group name>
22 unmonitor <watch or group name>
24 Options:
25 EOF
27 opts.on("-cCONFIG", "--config-file CONFIG", "Configuration file") do |x|
28 options[:config] = x
29 end
31 opts.on("-pPORT", "--port PORT", "Communications port") do |x|
32 options[:port] = x
33 end
35 opts.on("-D", "--no-daemonize", "Don't daemonize") do
36 options[:daemonize] = false
37 end
39 opts.on("-v", "--version", "Print the version number and exit") do
40 options[:version] = true
41 end
42 end.parse!
44 if options[:version]
45 # disable at_exit
46 module God; def self.at_exit; end; end
48 # print version
49 puts "Version #{God::VERSION}"
50 exit
51 elsif command = ARGV[0]
52 # a command was specified
54 # disable at_exit
55 module God; def self.at_exit; end; end
57 # get the name of the watch/group
58 name = ARGV[1]
60 # connect to remote drb
61 DRb.start_service
62 server = DRbObject.new nil, "druby://localhost:#{options[:port]}"
64 begin
65 puts "Sending '#{command}' command"
67 # send command
68 watches = server.control(name, command)
70 # output response
71 puts 'The following watches were affected:'
72 watches.each do |w|
73 puts ' ' + w.name
74 end
75 rescue God::InvalidCommandError
76 abort "Command '#{command}' is not valid. Run 'god --help' for usage"
77 end
78 else
79 # start god
80 if !options[:daemonize]
81 load File.expand_path(options[:config])
82 else
83 pid = fork do
84 begin
85 # Dir.chdir "/"
86 STDIN.reopen "/dev/null"
87 STDOUT.reopen('god.log', "a")
88 STDERR.reopen STDOUT
90 puts "Starting god"
92 unless God::EventHandler.loaded?
93 puts
94 puts "***********************************************************************"
95 puts "*"
96 puts "* Event conditions are not available for your installation of god."
97 puts "* You may still use and write custom conditions using the poll system"
98 puts "*"
99 puts "***********************************************************************"
100 puts
103 puts "Resetting file descriptors"
105 # Daemons.daemonize if options[:daemonize]
107 puts "Loading config"
109 load File.expand_path(options[:config])
110 rescue => e
111 File.open('god.log', 'a') { |f| f.puts e.message + "\n" + e.backtrace }
112 abort "!!! ERROR !!!"
116 File.open("god.pid", 'w') { |f| f.write pid }
118 ::Process.detach pid
120 # disable at_exit
121 module God; def self.at_exit; end; end