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>] [-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
32 terminate kill god and all tasks
37 opts
.on("-cCONFIG", "--config-file CONFIG", "Configuration file") do |x
|
41 opts
.on("-pPORT", "--port PORT", "Communications port") do |x
|
45 opts
.on("-PFILE", "--pid FILE", "Where to write the PID file") do |x
|
49 opts
.on("-lFILE", "--log FILE", "Where to write the log file") do |x
|
53 opts
.on("-D", "--no-daemonize", "Don't daemonize") do
54 options
[:daemonize] = false
57 opts
.on("-v", "--version", "Print the version number and exit") do
58 options
[:version] = true
61 opts
.on("-V", "Print extended version and build information") do
72 puts
"Version #{God::VERSION}"
77 puts
"Version: #{God::VERSION}"
79 puts
"Events: " + God
::EventHandler.event_system
82 elsif command
= ARGV[0]
85 # a command was specified
87 # connect to remote drb
89 server
= DRbObject
.new
nil, "druby://127.0.0.1:#{options[:port]}"
93 rescue DRb
::DRbConnError
94 puts
"The server is not available (or you do not have permissions to access it)"
98 puts e
.backtrace
.join("\n")
105 puts
"Sending '#{command}' command"
107 code
= File
.read(file
)
109 watches
= server
.running_load(code
)
112 puts
'The following watches were affected:'
116 elsif command
== 'status'
118 watches
= server
.status
119 watches
.keys
.sort
.each
do |name
|
120 state
= watches
[name
][:state]
121 puts
"#{name}: #{state}"
125 puts e
.backtrace
.join("\n")
127 elsif command
== 'log'
129 Signal
.trap('INT') { exit
!(0) }
133 print server
.running_log(name
, t
)
137 rescue God
::NoSuchWatchError
139 rescue DRb
::DRbConnError
140 puts
"The server went away"
143 puts e
.backtrace
.join("\n")
145 elsif command
== 'terminate'
147 t
= Thread
.new
{ loop { STDOUT.print('.'); STDOUT.flush
; sleep(1) } }
150 puts
'Stopped all watches'
153 puts
'Could not stop all watches within 10 seconds'
157 puts e
.backtrace
.join("\n")
162 abort
'Could not stop god'
163 rescue DRb
::DRbConnError
167 # get the name of the watch/group
171 puts
"Sending '#{command}' command"
173 t
= Thread
.new
{ loop { sleep(1); STDOUT.print('.'); STDOUT.flush
; sleep(1) } }
176 watches
= server
.control(name
, command
)
180 puts
'The following watches were affected:'
184 rescue God
::InvalidCommandError
185 t
.kill
rescue nil; STDOUT.puts
186 abort
"Command '#{command}' is not valid. Run 'god --help' for usage"
189 puts e
.backtrace
.join("\n")
194 elsif options
[:config]
196 if !options
[:daemonize]
200 God
.port
= options
[:port]
204 load File
.expand_path(options
[:config])
207 puts e
.backtrace
.join("\n")
208 abort
"There was an error in your configuration file (see above)"
211 # trap and ignore SIGHUP
212 Signal
.trap('HUP') {}
218 log_file
= options
[:log] || "/dev/null"
220 unless God
::EventHandler.loaded
?
222 puts
"***********************************************************************"
224 puts
"* Event conditions are not available for your installation of god."
225 puts
"* You may still use and write custom conditions using the poll system"
227 puts
"***********************************************************************"
231 # set port if requested
233 God
.port
= options
[:port]
238 load File
.expand_path(options
[:config])
241 puts e
.backtrace
.join("\n")
242 abort
"There was an error in your configuration file (see above)"
245 # reset file descriptors
246 STDIN.reopen
"/dev/null"
247 STDOUT.reopen(log_file
, "a")
251 puts e
.backtrace
.join("\n")
252 abort
"There was a fatal system error while starting god (see above)"
257 File
.open(options
[:pid], 'w') { |f
| f
.write pid
}