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!

4 Answers 4

18

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

4
  • 2
    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. May 5, 2011 at 0:40
  • @brandon craig rhodes is correct. No need to create a variable.
    – lysdexia
    May 12, 2011 at 19:03
  • 3
    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.
    – lysdexia
    May 24, 2011 at 20:15
  • 1
    Changed to reflect @BrandonRhodes critique.
    – lysdexia
    Dec 5, 2013 at 16:57
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

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.

1
  • The code in this tutorial was really helpful, thanks Dec 11, 2015 at 17:52
1

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()

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.