Question

Using a very simple BaseHttpServer (code below) I am experiencing the following problem:

Starting the server in background using ./testserver.py & works fine and the server responses on port 12121 . When I type in disown the server still responses. After closing the terminal the server stops after the next request with Input/output error in test.log

Steps to reconstruct:

$ ./testserver &
$ bg
$ disown

close terminal, send a request to server -> server does not respond.

The only solution I found was to redirect the stdout and stderr: $ ./testserver > /dev/null 2>&1 or as @Daniel Stated to call it the usual way with nohup

Has anyone experienced this bug before or why is this a desired behaviour for a HttpServer to crash if there is no output possible?


testserver.py

#! /usr/bin/env python

import time
import BaseHTTPServer


HOST_NAME = '0.0.0.0'
PORT_NUMBER = 12121


class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_HEAD(s):
        s.send_response(200)
        s.send_header("Content-type", "text/html")
        s.end_headers()
    def do_GET(s):
        """Respond to a GET request."""
        s.send_response(200)
        s.send_header("Content-type", "text/html")
        s.end_headers()
        s.wfile.write("MANUAL SERVER SUCCESS")

if __name__ == '__main__':
    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
    try:
        httpd.serve_forever()
    except Exception as e:
        with open('test.log', 'w') as f:
            f.write(str(e))
Was it helpful?

Solution

You only can write strings to files not exceptions. And you have to redirect stdout and stderr somewhere, otherwise any output will hang your program. Why don't you use nohup? That's the normal way, to start programs without terminal.

To make this clear: There is no special behavior in HttpServer. HttpServer is writing log-information to the terminal, so you need a terminal or a redirection to a file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top