Skip to content

Instantly share code, notes, and snippets.

@DeaconDesperado
Last active August 12, 2016 23:57
Show Gist options
  • Save DeaconDesperado/6295116 to your computer and use it in GitHub Desktop.
Save DeaconDesperado/6295116 to your computer and use it in GitHub Desktop.
Run cherrypy WSGI server as a daemon
from cherrypy.process.plugins import Daemonizer,PIDFile
import cherrypy
import argparse
parser = argparse.ArgumentParser(description="My server daemon")
parser.add_argument('-d','--daemon',help='Run the server as a daemon using the traditional double fork',action='store_true')
parser.add_argument('-a','--bind-address',help='Network interface to bind to',default='127.0.0.1')
parser.add_argument('-p','--port',help='Port to bind to',default=8080,type=int)
parser.add_argument('--pidfile',help='process id file',type=str)
args = parser.parse_args()
#this is your wsgi app: it could be a flask, werkzeug, pylons app etc
def application(environ, start_response):
start_response('200 OK', [('content-type', 'text/plain')])
return ('Hello world!',)
cherrypy.config.update({
'server.socket_host':args.bind_address,
'server.socket_port':args.port
})
cherrypy.tree.graft(application,'/')
if args.daemon:
Daemonizer(cherrypy.engine).subscribe()
if args.pidfile:
PIDFile(cherrypy.engine,args.pidfile).subscribe()
cherrypy.engine.start()
@ItsMeooooooo
Copy link

Why do you not use the 'cherryd -d' command for daemonize?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment