Deploying CherryPy (daemon)
Asked Answered
C

4

26

I've followed the basic CherryPy tutorial (http://www.cherrypy.org/wiki/CherryPyTutorial). One thing not discussed is deployment.

How can I launch a CherryPy app as a daemon and "forget about it"? What happens if the server reboots?

Is there a standard recipe? Maybe something that will create a service script (/etc/init.d/cherrypy...)

Thanks!

Coralyn answered 23/9, 2009 at 1:27 Comment(0)
V
14

There is a Daemonizer plugin for CherryPy included by default which is useful for getting it to start but by far the easiest way for simple cases is to use the cherryd script:

> cherryd -h
Usage: cherryd [options]

Options:
  -h, --help            show this help message and exit
  -c CONFIG, --config=CONFIG
                        specify config file(s)
  -d                    run the server as a daemon
  -e ENVIRONMENT, --environment=ENVIRONMENT
                        apply the given config environment
  -f                    start a fastcgi server instead of the default HTTP
                        server
  -s                    start a scgi server instead of the default HTTP server
  -i IMPORTS, --import=IMPORTS
                        specify modules to import
  -p PIDFILE, --pidfile=PIDFILE
                        store the process id in the given file

As far as an init.d script goes I think there are examples that can be Googled.

And the cherryd is found in your:

virtualenv/lib/python2.7/site-packages/cherrypy/cherryd

or in: https://bitbucket.org/cherrypy/cherrypy/src/default/cherrypy/cherryd

Vertebra answered 23/9, 2009 at 1:44 Comment(0)
G
19

Daemonizer can be pretty simple to use:

# this works for cherrypy 3.1.2 on Ubuntu 10.04
from cherrypy.process.plugins import Daemonizer
# before mounting anything
Daemonizer(cherrypy.engine).subscribe()

cherrypy.tree.mount(MyDaemonApp, "/")
cherrypy.engine.start()
cherrypy.engine.block()

There is a decent HOWTO for SysV style here.

To summarize:

  1. Create a file named for your application in /etc/init.d that calls /bin/sh

    sudo vim /etc/init.d/MyDaemonApp

    #!/bin/sh  
    echo "Invoking MyDaemonApp";  
    /path/to/MyDaemonApp  
    echo "Started MyDaemonApp. Tremble, Ye Mighty."  
    
  2. Make it executable

    sudo chmod +x /etc/init.d/MyDaemonApp

  3. Run update-rc.d to create our proper links in the proper runtime dir.

    sudo update-rc.d MyDaemonApp defaults 80

  4. sudo /etc/init.d/MyDaemonApp

Gormless answered 18/3, 2011 at 14:22 Comment(4)
I have no idea what a spizouzou is, but the variable is entirely unnecessary. Simply say: Daemonizer(cherrypy.engine).subscribe() and the plugin will persist because the engine now keeps a reference to it alive.Nosey
@brandon craig rhodes is correct. No need to create a variable.Gormless
tools.cherrypy.org/wiki/WindowsService As mentioned elsewhere, one can create a windows service with about the same amount of effort. Pay attention to the logging bits at the bottom of the page! They are important.Gormless
Changed to reflect @BrandonRhodes critique.Gormless
V
14

There is a Daemonizer plugin for CherryPy included by default which is useful for getting it to start but by far the easiest way for simple cases is to use the cherryd script:

> cherryd -h
Usage: cherryd [options]

Options:
  -h, --help            show this help message and exit
  -c CONFIG, --config=CONFIG
                        specify config file(s)
  -d                    run the server as a daemon
  -e ENVIRONMENT, --environment=ENVIRONMENT
                        apply the given config environment
  -f                    start a fastcgi server instead of the default HTTP
                        server
  -s                    start a scgi server instead of the default HTTP server
  -i IMPORTS, --import=IMPORTS
                        specify modules to import
  -p PIDFILE, --pidfile=PIDFILE
                        store the process id in the given file

As far as an init.d script goes I think there are examples that can be Googled.

And the cherryd is found in your:

virtualenv/lib/python2.7/site-packages/cherrypy/cherryd

or in: https://bitbucket.org/cherrypy/cherrypy/src/default/cherrypy/cherryd

Vertebra answered 23/9, 2009 at 1:44 Comment(0)
D
5

I wrote a tutorial/project skeleton, cherrypy-webapp-skeleton, which goal was to fill the gaps for deploying a real-world CherryPy application on Debian* for a web-developer. It features extended cherryd for daemon privilege drop. There's also a number of important script and config files for init.d, nginx, monit, logrotate. The tutorial part describes how to put things together and eventually forget about it. The skeleton part proposes a way of possible arrangement of CherryPy webapp project assets.


* It was written for Squeeze but practically it should be same for Wheezy.

Disbranch answered 28/6, 2014 at 13:27 Comment(1)
The code in this tutorial was really helpful, thanksCastano
Z
2

Info on Daemonizer options

When using Daemonizer, the docs don't state the options, e.g. how to redirect stdout or stderr. From the source of the Daemonizer class you can find the options. As a reference take this example from my project:

# run server as a daemon
d = Daemonizer(cherrypy.engine,
               stdout='/home/pi/Gate/log/gate_access.log',
               stderr='/home/pi/Gate/log/gate_error.log')
d.subscribe()
Zenda answered 11/11, 2017 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.