I run python CGIHTTPServer with (server.py)
#!/usr/bin/env python
import CGIHTTPServer
def main():
server_address = ('', 8000)
handler = CGIHTTPServer.CGIHTTPRequestHandler
handler.cgi_directories = ['/cgi']
server = CGIHTTPServer.BaseHTTPServer.HTTPServer(server_address, handler)
try:
server.serve_forever()
except KeyboardInterrupt:
server.socket.close()
if __name__ == '__main__':
main()
I try to make a linux service with
#!/bin/sh
case $1 in
start)
./server.py
;;
stop) # code to stop the service
esac
QUESTION 1: When I start the service, it will live in the terminal; not returning to the next bash command line. Thus, the service will stopped upon closing the terminal. How can I keep the web server active even after closing the terminal?
QUESTION 2: How can I stop this service? (after keeping it on by Question 1)
Start your program in background with:
nohup ./server.py &
The preferred way to stop your service is to issue a kill command with your service’s PID as argument.
P.S.
start-stop-daemon metioned in comments is better but slightly more complex.