Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3dbfbbd

Browse files
committedJul 8, 2015
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>
1 parent 2466123 commit 3dbfbbd

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed
 

‎CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ New in master
44
Features
55
--------
66

7+
* Add ``-d``, ``--detach`` option for ``nikola serve``
78
* Delete old ``bootstrap`` theme (use ``bootstrap3`` instead)
89
* Screen reader-friendly navbar collapses and dropdowns (Issue #1863)
910
* Modern reST stylesheets, based in part on Bootstrap 3 (Issue #1150)

‎docs/man/nikola.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ The most basic commands needed to get by are:
9494
deploy the site using the ``DEPLOY_COMMANDS`` setting
9595
``nikola github_deploy```
9696
deploy the site to GitHub Pages
97-
``nikola serve [-p PORT] [-a ADDRESS] [-b|--browser] [-6|--ipv6]``
97+
``nikola serve [-p PORT] [-a ADDRESS] [-d|--detach] [-b|--browser] [-6|--ipv6]``
9898
start development web server
9999
``nikola auto [-p PORT] [-a ADDRESS] [-b|--browser] [-6|--ipv6]``
100100
start development web server with automated rebuilds and reloads

‎nikola/plugins/command/serve.py

+31-5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ class CommandServe(Command):
6969
'default': '',
7070
'help': 'Address to bind (default: 0.0.0.0 – all local IPv4 interfaces)',
7171
},
72+
{
73+
'name': 'detach',
74+
'short': 'd',
75+
'long': 'detach',
76+
'type': bool,
77+
'default': False,
78+
'help': 'Detach from TTY (work in the background)',
79+
},
7280
{
7381
'name': 'browser',
7482
'short': 'b',
@@ -117,16 +125,34 @@ def _execute(self, options, args):
117125
server_url = "http://{0}:{1}/".format(*sa)
118126
self.logger.info("Opening {0} in the default web browser...".format(server_url))
119127
webbrowser.open(server_url)
120-
try:
121-
httpd.serve_forever()
122-
except KeyboardInterrupt:
123-
self.logger.info("Server is shutting down.")
124-
return 130
128+
if options['detach']:
129+
OurHTTPRequestHandler.quiet = True
130+
pid = os.fork()
131+
if pid == 0:
132+
httpd.serve_forever()
133+
else:
134+
self.logger.info("Detached with PID {0}. Run `kill {0}` to stop the server.".format(pid))
135+
else:
136+
try:
137+
httpd.serve_forever()
138+
except KeyboardInterrupt:
139+
self.logger.info("Server is shutting down.")
140+
return 130
125141

126142

127143
class OurHTTPRequestHandler(SimpleHTTPRequestHandler):
144+
"""A request handler, modified for Nikola."""
128145
extensions_map = dict(SimpleHTTPRequestHandler.extensions_map)
129146
extensions_map[""] = "text/plain"
147+
quiet = False
148+
149+
def log_message(self, *args):
150+
"""Log messages. Or not, depending on a setting."""
151+
if self.quiet:
152+
return
153+
else:
154+
# Old-style class in Python 2.7, cannot use super()
155+
return SimpleHTTPRequestHandler.log_message(self, *args)
130156

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

0 commit comments

Comments
 (0)
Please sign in to comment.