Skip to content
Permalink
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
discarded.

    $ nikola serve -d
    [2015-07-08T08:20:25Z] INFO: serve: Serving HTTP on 0.0.0.0 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 <kwpolska@gmail.com>
  • 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/serve.py
@@ -4,6 +4,7 @@ New in master
Features
--------

* 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: 0.0.0.0 – 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)
self.logger.info("Opening {0} in the default web browser...".format(server_url))
webbrowser.open(server_url)
try:
httpd.serve_forever()
except KeyboardInterrupt:
self.logger.info("Server is shutting down.")
return 130
if options['detach']:
OurHTTPRequestHandler.quiet = True
pid = os.fork()
if pid == 0:
httpd.serve_forever()
else:
self.logger.info("Detached with PID {0}. Run `kill {0}` to stop the server.".format(pid))
else:
try:
httpd.serve_forever()
except KeyboardInterrupt:
self.logger.info("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:
return
else:
# 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.