Skip to content
Browse files
Add -d, --detach to nikola serve
This is a clone of `jekyll serve --detach`.  The feature will return
control to your terminal immediately, letting you work on your website
with the server running in the background.  This is accomplished by
forking right before the server is initialized. All log messages are

    $ nikola serve -d
    [2015-07-08T08:20:25Z] INFO: serve: Serving HTTP on port 8000...
    [2015-07-08T08:20:25Z] INFO: serve: Detached with PID 14594. Run `kill 14594` to stop the server.

This is functionally equivalent to `nikola serve 2> /dev/null &`, but
is shell-independent and easier to type.

Signed-off-by: Chris Warrick <>
  • Loading branch information
Kwpolska committed Jul 8, 2015
1 parent 2466123 commit 3dbfbbdce52ea0474d2dc8e6e19b42ccba2b2e1e
Showing with 33 additions and 6 deletions.
  1. +1 −0 CHANGES.txt
  2. +1 −1 docs/man/nikola.rst
  3. +31 −5 nikola/plugins/command/
@@ -4,6 +4,7 @@ New in master

* Add ``-d``, ``--detach`` option for ``nikola serve``
* Delete old ``bootstrap`` theme (use ``bootstrap3`` instead)
* Screen reader-friendly navbar collapses and dropdowns (Issue #1863)
* Modern reST stylesheets, based in part on Bootstrap 3 (Issue #1150)
@@ -94,7 +94,7 @@ The most basic commands needed to get by are:
deploy the site using the ``DEPLOY_COMMANDS`` setting
``nikola github_deploy```
deploy the site to GitHub Pages
``nikola serve [-p PORT] [-a ADDRESS] [-b|--browser] [-6|--ipv6]``
``nikola serve [-p PORT] [-a ADDRESS] [-d|--detach] [-b|--browser] [-6|--ipv6]``
start development web server
``nikola auto [-p PORT] [-a ADDRESS] [-b|--browser] [-6|--ipv6]``
start development web server with automated rebuilds and reloads
@@ -69,6 +69,14 @@ class CommandServe(Command):
'default': '',
'help': 'Address to bind (default: – all local IPv4 interfaces)',
'name': 'detach',
'short': 'd',
'long': 'detach',
'type': bool,
'default': False,
'help': 'Detach from TTY (work in the background)',
'name': 'browser',
'short': 'b',
@@ -117,16 +125,34 @@ def _execute(self, options, args):
server_url = "http://{0}:{1}/".format(*sa)"Opening {0} in the default web browser...".format(server_url))
except KeyboardInterrupt:"Server is shutting down.")
return 130
if options['detach']:
OurHTTPRequestHandler.quiet = True
pid = os.fork()
if pid == 0:
else:"Detached with PID {0}. Run `kill {0}` to stop the server.".format(pid))
except KeyboardInterrupt:"Server is shutting down.")
return 130

class OurHTTPRequestHandler(SimpleHTTPRequestHandler):
"""A request handler, modified for Nikola."""
extensions_map = dict(SimpleHTTPRequestHandler.extensions_map)
extensions_map[""] = "text/plain"
quiet = False

def log_message(self, *args):
"""Log messages. Or not, depending on a setting."""
if self.quiet:
# Old-style class in Python 2.7, cannot use super()
return SimpleHTTPRequestHandler.log_message(self, *args)

# NOTICE: this is a patched version of send_head() to disable all sorts of
# caching. `nikola serve` is a development server, hence caching should

0 comments on commit 3dbfbbd

Please sign in to comment.