3 $
:.unshift File
.join(File
.dirname(__FILE__
), *%w
[.. lib
])
9 options
= {:daemonize => true, :port => 17165}
11 opts
= OptionParser
.new
do |opts
|
15 god -c <config file> [-p <port> | -b] [-P <file>] [-l <file>] [-D]
18 god <command> <argument> [-p <port>]
19 god <command> [-p <port>]
21 god -V (must be run as root to be accurate on Linux)
24 start <task or group name> start task or group
25 restart <task or group name> restart task or group
26 stop <task or group name> stop task or group
27 monitor <task or group name> monitor task or group
28 unmonitor <task or group name> unmonitor task or group
29 load <file> load a config into a running god
30 log <task name> show realtime log for given task
31 status show status of each task
33 terminate stop god and all tasks
38 opts
.on("-cCONFIG", "--config-file CONFIG", "Configuration file") do |x
|
42 opts
.on("-pPORT", "--port PORT", "Communications port (default 17165)") do |x
|
46 opts
.on("-b", "--auto-bind", "Auto-bind to an unused port number") do
50 opts
.on("-PFILE", "--pid FILE", "Where to write the PID file") do |x
|
54 opts
.on("-lFILE", "--log FILE", "Where to write the log file") do |x
|
58 opts
.on("-D", "--no-daemonize", "Don't daemonize") do
59 options
[:daemonize] = false
62 opts
.on("-v", "--version", "Print the version number and exit") do
63 options
[:version] = true
66 opts
.on("-V", "Print extended version and build information") do
77 puts
"Version #{God::VERSION}"
82 puts
"Version: #{God::VERSION}"
84 puts
"Events: " + God
::EventHandler.event_system
87 elsif command
= ARGV[0]
90 # a command was specified
92 # connect to remote drb
94 server
= DRbObject
.new
nil, "druby://127.0.0.1:#{options[:port]}"
98 rescue DRb
::DRbConnError
99 puts
"The server is not available (or you do not have permissions to access it)"
103 puts e
.backtrace
.join("\n")
110 puts
"Sending '#{command}' command"
112 code
= File
.read(file
)
114 watches
= server
.running_load(code
)
117 puts
'The following watches were affected:'
121 elsif command
== 'status'
123 watches
= server
.status
124 watches
.keys
.sort
.each
do |name
|
125 state
= watches
[name
][:state]
126 puts
"#{name}: #{state}"
130 puts e
.backtrace
.join("\n")
132 elsif command
== 'log'
134 Signal
.trap('INT') { exit
!(0) }
138 print server
.running_log(name
, t
)
142 rescue God
::NoSuchWatchError
144 rescue DRb
::DRbConnError
145 puts
"The server went away"
148 puts e
.backtrace
.join("\n")
150 elsif command
== 'quit'
153 abort
'Could not stop god'
154 rescue DRb
::DRbConnError
157 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'
169 puts e
.backtrace
.join("\n")
174 abort
'Could not stop god'
175 rescue DRb
::DRbConnError
179 # get the name of the watch/group
183 puts
"Sending '#{command}' command"
185 t
= Thread
.new
{ loop { sleep(1); STDOUT.print('.'); STDOUT.flush
; sleep(1) } }
188 watches
= server
.control(name
, command
)
192 puts
'The following watches were affected:'
196 rescue God
::InvalidCommandError
197 t
.kill
rescue nil; STDOUT.puts
198 abort
"Command '#{command}' is not valid. Run 'god --help' for usage"
201 puts e
.backtrace
.join("\n")
206 elsif options
[:config]
208 if !options
[:daemonize]
212 God
.port
= options
[:port]
216 load File
.expand_path(options
[:config])
219 puts e
.backtrace
.join("\n")
220 abort
"There was an error in your configuration file (see above)"
223 # trap and ignore SIGHUP
224 Signal
.trap('HUP') {}
230 log_file
= options
[:log] || "/dev/null"
232 unless God
::EventHandler.loaded
?
234 puts
"***********************************************************************"
236 puts
"* Event conditions are not available for your installation of god."
237 puts
"* You may still use and write custom conditions using the poll system"
239 puts
"***********************************************************************"
243 # set port if requested
245 God
.port
= options
[:port]
250 load File
.expand_path(options
[:config])
253 puts e
.backtrace
.join("\n")
254 abort
"There was an error in your configuration file (see above)"
257 # reset file descriptors
258 STDIN.reopen
"/dev/null"
259 STDOUT.reopen(log_file
, "a")
263 puts e
.backtrace
.join("\n")
264 abort
"There was a fatal system error while starting god (see above)"
269 File
.open(options
[:pid], 'w') { |f
| f
.write pid
}