respect "working_directory" wrt config.ru
[unicorn.git] / bin / unicorn
blob2cc10b1162797a190dc06e1df8a1fbc04ac0d00f
1 #!/this/will/be/overwritten/or/wrapped/anyways/do/not/worry/ruby
2 # -*- encoding: binary -*-
3 require 'unicorn/launcher'
4 require 'optparse'
6 ENV["RACK_ENV"] ||= "development"
7 daemonize = false
8 options = { :listeners => [] }
9 host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT
10 set_listener = false
12 opts = OptionParser.new("", 24, ' ') do |opts|
13 opts.banner = "Usage: #{File.basename($0)} " \
14 "[ruby options] [unicorn options] [rackup config file]"
16 opts.separator "Ruby options:"
18 lineno = 1
19 opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
20 eval line, TOPLEVEL_BINDING, "-e", lineno
21 lineno += 1
22 end
24 opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
25 $DEBUG = true
26 end
28 opts.on("-w", "--warn", "turn warnings on for your script") do
29 $-w = true
30 end
32 opts.on("-I", "--include PATH",
33 "specify $LOAD_PATH (may be used more than once)") do |path|
34 $LOAD_PATH.unshift(*path.split(/:/))
35 end
37 opts.on("-r", "--require LIBRARY",
38 "require the library, before executing your script") do |library|
39 require library
40 end
42 opts.separator "Unicorn options:"
44 # some of these switches exist for rackup command-line compatibility,
46 opts.on("-o", "--host HOST",
47 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h|
48 host = h
49 set_listener = true
50 end
52 opts.on("-p", "--port PORT",
53 "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |p|
54 port = p.to_i
55 set_listener = true
56 end
58 opts.on("-E", "--env ENVIRONMENT",
59 "use ENVIRONMENT for defaults (default: development)") do |e|
60 ENV["RACK_ENV"] = e
61 end
63 opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
64 daemonize = d ? true : false
65 end
67 opts.on("-P", "--pid FILE", "DEPRECATED") do |f|
68 warn %q{Use of --pid/-P is strongly discouraged}
69 warn %q{Use the 'pid' directive in the Unicorn config file instead}
70 options[:pid] = f
71 end
73 opts.on("-s", "--server SERVER",
74 "this flag only exists for compatibility") do |s|
75 warn "-s/--server only exists for compatibility with rackup"
76 end
78 # Unicorn-specific stuff
79 opts.on("-l", "--listen {HOST:PORT|PATH}",
80 "listen on HOST:PORT or PATH",
81 "this may be specified multiple times",
82 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
83 options[:listeners] << address
84 end
86 opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
87 options[:config_file] = f
88 end
90 # I'm avoiding Unicorn-specific config options on the command-line.
91 # IMNSHO, config options on the command-line are redundant given
92 # config files and make things unnecessarily complicated with multiple
93 # places to look for a config option.
95 opts.separator "Common options:"
97 opts.on_tail("-h", "--help", "Show this message") do
98 puts opts.to_s.gsub(/^.*DEPRECATED.*$/s, '')
99 exit
102 opts.on_tail("-v", "--version", "Show version") do
103 puts "unicorn v#{Unicorn::Const::UNICORN_VERSION}"
104 exit
107 opts.parse! ARGV
110 app = Unicorn.builder(ARGV[0] || 'config.ru', opts)
111 options[:listeners] << "#{host}:#{port}" if set_listener
113 if $DEBUG
114 require 'pp'
115 pp({
116 :unicorn_options => options,
117 :app => app,
118 :daemonize => daemonize,
122 Unicorn::Launcher.daemonize!(options) if daemonize
123 Unicorn.run(app, options)