Saturday, July 02, 2011

cherrypy daemontools

Cherrypy and daemontools work nicely together. However, I have not found any instructions on how to use them together on the interwebs... so I've written up some basic notes for people who love cherrypy and daemontools.


daemontools is a collection of tools for managing UNIX services. It can be used to automatically start, and restart processes on a unix system.

The instructions to run services in daemontools are here: http://cr.yp.to/daemontools/faq/create.html#run. I'm assuming you have daemontools set up already.

Cherrypy is a python library for making website applications. There are many ways to deploy cherrypy apps, but I will describe how to deploy cherrypy apps with daemontools.

Firstly, you do not need the cherryd tool to use with daemontools. Daemontools does the daemonising for you. But cherryd can be used if you want, since it provides some nice options (just don't use the -d daemonize flag). You may want to use cherryd with daemontools if you'd like to use FastCGI or SCGI. I won't cover cherryd any further in this post.

I'll now show you how to setup daemontools for an example cherrypy application.

Here is a normal helloworld cherrypy app... which I'll put in the file exampleapp.py
import cherrypy

class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True

cherrypy.quickstart(HelloWorld())


This runs on 127.0.0.1 on port 8080 by default. It also runs in development mode (which you should not deploy with, but only use for testing).

Now create a 'theservicedir' directory somewhere.
Also create the following directories: 'theservicedir/log' and 'theservicedir/log/main'


Then create a 'theservicedir/run' file.
#!/bin/sh
exec setuidgid rene /usr/bin/python /home/rene/cherrypydaemontools/exampleapp.py


Note that I specified full paths to python and to your python script. Also see that I got the app to run with the setuidgid program as the user 'rene'.

Then create a 'theservicedir/run' file for logging.
#!/bin/sh
exec setuidgid rene multilog t ./main


Now to start it up we symlink the file into the service directory.
sudo ln -s /home/rene/cherrypydaemontools/theservicedir /service/theservicedir


To stop it:
svc -d /service/theservicedir

To restart it:
svc -t /service/theservicedir

To start it:
svc -u /service/theservicedir

Your app should start up the next time your server reboots. Happy cherrypy and daemontools usage. Please leave any related tips or fixes to these instructions in the comments.

No comments: