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
76 if !options
[:config] && options
[:version]
80 puts
"Version #{God::VERSION}"
82 elsif !options
[:config] && options
[:info]
85 puts
"Version: #{God::VERSION}"
87 puts
"Events: " + God
::EventHandler.event_system
90 elsif !options
[:config] && command
= ARGV[0]
93 # a command was specified
95 # connect to drb unix socket
97 server
= DRbObject
.new(nil, God
::Socket.socket(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])
219 rescue Exception
=> e
220 if e
.instance_of
?(SystemExit
)
224 puts e
.backtrace
.join("\n")
225 abort
"There was an error in your configuration file (see above)"
230 # trap and ignore SIGHUP
231 Signal
.trap('HUP') {}
237 log_file
= options
[:log] || "/dev/null"
239 unless God
::EventHandler.loaded
?
241 puts
"***********************************************************************"
243 puts
"* Event conditions are not available for your installation of god."
244 puts
"* You may still use and write custom conditions using the poll system"
246 puts
"***********************************************************************"
250 # set port if requested
252 God
.port
= options
[:port]
257 unless File
.exist
?(options
[:config])
258 abort
"File not found: #{options[:config]}"
262 load File
.expand_path(options
[:config])
263 rescue Exception
=> e
264 if e
.instance_of
?(SystemExit
)
268 puts e
.backtrace
.join("\n")
269 abort
"There was an error in your configuration file (see above)"
274 # reset file descriptors
275 STDIN.reopen
"/dev/null"
276 STDOUT.reopen(log_file
, "a")
280 puts e
.backtrace
.join("\n")
281 abort
"There was a fatal system error while starting god (see above)"
286 File
.open(options
[:pid], 'w') { |f
| f
.write pid
}
294 rescue Exception
=> e
295 if e
.instance_of
?(SystemExit
)
298 puts
'Uncaught exception'
300 puts e
.backtrace
.join("\n")