Revert "Remove -P/--pid switch from CLI"
[unicorn.git] / bin / unicorn
bloba13facca74372470bc6689fe1f5745e99f2c341d
1 #!/home/ew/bin/ruby
2 $stdin.sync = $stdout.sync = $stderr.sync = true
3 require 'unicorn' # require this first to populate Unicorn::DEFAULT_START_CTX
4 require 'optparse'
6 env = "development"
7 daemonize = false
8 listeners = []
9 options = { :listeners => listeners }
10 host = Unicorn::Const::DEFAULT_HOST
11 port = Unicorn::Const::DEFAULT_PORT
13 opts = OptionParser.new("", 24, ' ') do |opts|
14 opts.banner = "Usage: #{File.basename($0)} " \
15 "[ruby options] [unicorn options] [rackup config file]"
17 opts.separator "Ruby options:"
19 lineno = 1
20 opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
21 eval line, TOPLEVEL_BINDING, "-e", lineno
22 lineno += 1
23 end
25 opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
26 $DEBUG = true
27 end
29 opts.on("-w", "--warn", "turn warnings on for your script") do
30 $-w = true
31 end
33 opts.on("-I", "--include PATH",
34 "specify $LOAD_PATH (may be used more than once)") do |path|
35 $LOAD_PATH.unshift(*path.split(/:/))
36 end
38 opts.on("-r", "--require LIBRARY",
39 "require the library, before executing your script") do |library|
40 require library
41 end
43 opts.separator "Unicorn options:"
45 # some of these switches exist for rackup command-line compatibility,
47 opts.on("-o", "--host HOST",
48 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h|
49 host = h
50 end
52 opts.on("-p", "--port PORT",
53 "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |p|
54 port = p.to_i
55 end
57 opts.on("-E", "--env ENVIRONMENT",
58 "use ENVIRONMENT for defaults (default: development)") do |e|
59 env = e
60 end
62 opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
63 daemonize = d ? true : false
64 end
66 opts.on("-P", "--pid FILE", "file to store PID (default: none)") do |f|
67 options[:pid] = File.expand_path(f)
68 end
70 # Unicorn-specific stuff
71 opts.on("-l", "--listen {HOST:PORT|PATH}",
72 "listen on HOST:PORT or PATH",
73 "this may be specified multiple times",
74 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
75 listeners << address
76 end
78 opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
79 options[:config_file] = File.expand_path(f)
80 end
82 # I'm avoiding Unicorn-specific config options on the command-line.
83 # IMNSHO, config options on the command-line are redundant given
84 # config files and make things unnecessarily complicated with multiple
85 # places to look for a config option.
87 opts.separator "Common options:"
89 opts.on_tail("-h", "--help", "Show this message") do
90 puts opts
91 exit
92 end
94 opts.on_tail("-v", "--version", "Show version") do
95 puts "unicorn v#{Unicorn::Const::UNICORN_VERSION}"
96 exit
97 end
99 opts.parse! ARGV
102 require 'pp' if $DEBUG
104 # require Rack as late as possible in case $LOAD_PATH is modified
105 # in config.ru or command-line
106 require 'rack'
108 config = ARGV[0] || "config.ru"
109 abort "configuration file #{config} not found" unless File.exist?(config)
111 inner_app = case config
112 when /\.ru$/
113 raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
114 # parse embedded command-line options in config.ru comments
115 if raw[/^#\\(.*)/]
116 opts.parse! $1.split(/\s+/)
118 lambda { || eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config) }
119 else
120 lambda do ||
121 require config
122 Object.const_get(File.basename(config, '.rb').capitalize)
126 app = case env
127 when "development"
128 lambda do ||
129 Rack::Builder.new do
130 use Rack::CommonLogger, $stderr
131 use Rack::ShowExceptions
132 use Rack::Lint
133 run inner_app.call
134 end.to_app
136 when "deployment"
137 lambda do ||
138 Rack::Builder.new do
139 use Rack::CommonLogger, $stderr
140 run inner_app.call
141 end.to_app
143 else
144 inner_app
147 if listeners.empty?
148 listener = "#{host}:#{port}"
149 listeners << listener
152 if $DEBUG
153 pp({
154 :unicorn_options => options,
155 :app => app,
156 :inner_app => inner_app,
157 :daemonize => daemonize,
161 # only daemonize if we're not inheriting file descriptors from our parent
162 if daemonize
164 $stdin.reopen("/dev/null")
165 unless ENV['UNICORN_FD']
166 exit if fork
167 Process.setsid
168 exit if fork
171 # We don't do a lot of standard daemonization stuff:
172 # * $stderr/$stderr can/will be redirected separately
173 # * umask is whatever was set by the parent process at startup
174 # and can be set in config.ru and config_file, so making it
175 # 0000 and potentially exposing sensitive log data can be bad
176 # policy.
177 # * Don't bother to chdir here since Unicorn is designed to
178 # run inside APP_ROOT. Unicorn will also re-chdir() to
179 # the directory it was started in when being re-executed
180 # to pickup code changes if the original deployment directory
181 # is a symlink or otherwise got replaced.
184 Unicorn.run(app, options)