5 $
:.unshift File
.join(File
.dirname(__FILE__
), *%w
[.. lib
])
12 options
= {:daemonize => true, :port => 17165}
14 opts
= OptionParser
.new
do |opts
|
18 god -c <config file> [-p <port> | -b] [-P <file>] [-l <file>] [-D]
21 god <command> <argument> [-p <port>]
22 god <command> [-p <port>]
24 god -V (must be run as root to be accurate on Linux)
27 start <task or group name> start task or group
28 restart <task or group name> restart task or group
29 stop <task or group name> stop task or group
30 monitor <task or group name> monitor task or group
31 unmonitor <task or group name> unmonitor task or group
32 load <file> load a config into a running god
33 log <task name> show realtime log for given task
34 status show status of each task
36 terminate stop god and all tasks
41 opts
.on("-cCONFIG", "--config-file CONFIG", "Configuration file") do |x
|
45 opts
.on("-pPORT", "--port PORT", "Communications port (default 17165)") do |x
|
49 opts
.on("-b", "--auto-bind", "Auto-bind to an unused port number") do
53 opts
.on("-PFILE", "--pid FILE", "Where to write the PID file") do |x
|
57 opts
.on("-lFILE", "--log FILE", "Where to write the log file") do |x
|
61 opts
.on("-D", "--no-daemonize", "Don't daemonize") do
62 options
[:daemonize] = false
65 opts
.on("-v", "--version", "Print the version number and exit") do
66 options
[:version] = true
69 opts
.on("-V", "Print extended version and build information") do
80 puts
"Version #{God::VERSION}"
85 puts
"Version: #{God::VERSION}"
87 puts
"Events: " + God
::EventHandler.event_system
90 elsif command
= ARGV[0]
93 # a command was specified
95 # connect to remote drb
97 server
= DRbObject
.new
nil, "druby://127.0.0.1:#{options[:port]}"
101 rescue DRb
::DRbConnError
102 puts
"The server is not available (or you do not have permissions to access it)"
109 puts
"Sending '#{command}' command"
112 unless File
.exist
?(file
)
113 abort
"File not found: #{file}"
116 names
, errors
= *server
.running_load(File
.read(file
), File
.expand_path(file
))
120 puts
'The following tasks were affected:'
130 elsif command
== 'status'
131 watches
= server
.status
132 watches
.keys
.sort
.each
do |name
|
133 state
= watches
[name
][:state]
134 puts
"#{name}: #{state}"
136 elsif command
== 'log'
138 Signal
.trap('INT') { exit
}
142 print server
.running_log(name
, t
)
146 rescue God
::NoSuchWatchError
148 rescue DRb
::DRbConnError
149 puts
"The server went away"
151 elsif command
== 'quit'
154 abort
'Could not stop god'
155 rescue DRb
::DRbConnError
158 elsif command
== 'terminate'
159 t
= Thread
.new
{ loop { STDOUT.print('.'); STDOUT.flush
; sleep(1) } }
162 puts
'Stopped all watches'
165 puts
'Could not stop all watches within 10 seconds'
170 abort
'Could not stop god'
171 rescue DRb
::DRbConnError
175 # get the name of the watch/group
179 puts
"Sending '#{command}' command"
181 t
= Thread
.new
{ loop { sleep(1); STDOUT.print('.'); STDOUT.flush
; sleep(1) } }
184 watches
= server
.control(name
, command
)
188 unless watches
.empty
?
189 puts
'The following watches were affected:'
194 puts
'No matching task or group'
196 rescue God
::InvalidCommandError
197 t
.kill
rescue nil; STDOUT.puts
198 abort
"Command '#{command}' is not valid. Run 'god --help' for usage"
205 if !options
[:daemonize]
209 God
.port
= options
[:port]
213 unless File
.exist
?(options
[:config])
214 abort
"File not found: #{options[:config]}"
218 load File
.expand_path(options
[:config])
221 puts e
.backtrace
.join("\n")
222 abort
"There was an error in your configuration file (see above)"
226 # trap and ignore SIGHUP
227 Signal
.trap('HUP') {}
233 log_file
= options
[:log] || "/dev/null"
235 unless God
::EventHandler.loaded
?
237 puts
"***********************************************************************"
239 puts
"* Event conditions are not available for your installation of god."
240 puts
"* You may still use and write custom conditions using the poll system"
242 puts
"***********************************************************************"
246 # set port if requested
248 God
.port
= options
[:port]
253 unless File
.exist
?(options
[:config])
254 abort
"File not found: #{options[:config]}"
258 load File
.expand_path(options
[:config])
261 puts e
.backtrace
.join("\n")
262 abort
"There was an error in your configuration file (see above)"
266 # reset file descriptors
267 STDIN.reopen
"/dev/null"
268 STDOUT.reopen(log_file
, "a")
272 puts e
.backtrace
.join("\n")
273 abort
"There was a fatal system error while starting god (see above)"
278 File
.open(options
[:pid], 'w') { |f
| f
.write pid
}
286 rescue Exception
=> e
287 if e
.instance_of
?(SystemExit
)
291 puts e
.backtrace
.join("\n")