Make middleman's preview server a daemon

This commit is contained in:
Dennis Günnewig 2015-11-11 22:20:37 +01:00
parent 916a5a508b
commit 8c7e156bd4
2 changed files with 20 additions and 1 deletions

View file

@ -49,6 +49,11 @@ module Middleman::Cli
aliases: '-l', aliases: '-l',
default: 0.5, default: 0.5,
desc: 'Set file watcher latency, in seconds' desc: 'Set file watcher latency, in seconds'
class_option :daemon,
type: :boolean,
aliases: '-d',
default: false,
desc: 'Daemonize preview server'
# Start the server # Start the server
def server def server
@ -73,7 +78,8 @@ module Middleman::Cli
disable_watcher: options['disable_watcher'], disable_watcher: options['disable_watcher'],
reload_paths: options['reload_paths'], reload_paths: options['reload_paths'],
force_polling: options['force_polling'], force_polling: options['force_polling'],
latency: options['latency'] latency: options['latency'],
daemon: options['daemon']
} }
puts '== The Middleman is loading' puts '== The Middleman is loading'

View file

@ -58,6 +58,19 @@ module Middleman
app.execute_callbacks(:before_server, [ServerInformationCallbackProxy.new(server_information)]) app.execute_callbacks(:before_server, [ServerInformationCallbackProxy.new(server_information)])
if @options[:daemon]
# To output the child PID, let's make preview server a daemon by hand
if child_pid = fork
app.logger.info "== Middleman preview server is running in background with PID #{child_pid}"
Process.detach child_pid
exit 0
else
$stdout.reopen('/dev/null', 'w')
$stderr.reopen('/dev/null', 'w')
$stdin.reopen('/dev/null', 'r')
end
end
loop do loop do
@webrick.start @webrick.start