Wednesday, July 09, 2008

Linux: Running Paster under runit on Ubuntu

I needed to setup Paster, Pylon's default Web server, on Ubuntu. I had a heck of a hard time deciding whether I wanted to run it under Supervisor, daemontools, monit, runit, or a handmade rc script.

Supervisor has been getting a lot of press lately, but I couldn't find a standard way of starting it on Ubuntu. This seemed like a chicken and egg problem. Furthermore, I knew my buddy Mike Orr was having a hard time getting log rotation working with it.

I wanted something that could restart my process as necessary, so I decided against a standard rc script, even though I knew Paster has a --monitor-restart option. I eventually settled on runit. I had used it in the past, the directions were clear, and it just "felt right". My buddy Allan Bailey even gave me a quick cheat sheet.

If anyone can show me a nice comparison of all of these options, I'd love to read it. I wasn't able to find much on Google that compared them all.

Anyway, here's what I did in outline form:
Setup myapp under runit:
Setup logging:
mkdir -p /etc/sv/myapp/log
mkdir -p /var/log/myapp

cat > /etc/sv/myapp/log/run << __END__
#!/bin/sh

exec 2>&1
exec chpst -u www-data:www-data svlogd -tt /var/log/myapp
__END__

chmod +x /etc/sv/myapp/log/run
chown -R www-data:www-data /var/log/myapp
Setup Paster:

cat > /etc/sv/myapp/run << __END__
#!/bin/sh

exec 2>&1
cd /home/myproject/source/trunk/myapp
exec chpst -u www-data:www-data paster serve production.ini
__END__

chmod +x /etc/sv/myapp/run
ln -s /etc/sv/myapp /etc/service/
Setup /var for the app:
mkdir /var/run/myapp
chown -R www-data:www-data /var/run/myapp
Edited various directory settings in production.ini.

13 comments:

Shannon -jj Behrens said...

I have a user named myproject. In order to restart the daemon with that user instead of having to sudo to root (which would require a password that a script might not have), I did:

cd /etc/sv/myapp/supervise

chmod 755 .

chown myproject:myproject ok control status

Shannon -jj Behrens said...

It turns out that my VPS mounts /var/run on a tmpfs. Hence, I had to adjust:

cat > /etc/sv/myapp/run << __END__
#!/bin/sh

exec 2>&1
mkdir -p /var/run/myapp
chown -R www-data:www-data \
/var/run/myapp
cd /home/myproject/source/trunk/myapp
exec chpst -u www-data:www-data \
paster serve production.ini
__END__

Clint said...

The following 4 line script will work if your script returns the proper value (3 to restart).

import subprocess
r = 3
while r == 3:
r = subprocess.call(["python2.5","myscript.py"])

Shannon -jj Behrens said...

> The following 4 line script will work if your script returns the proper value (3 to restart).

Huh? What does that have to do with launching a daemon on boot?

Chris Arndt said...

You can find a debian/Ubuntu-compliant init.d script for supervisor on my website. See also my blog post on the topic.

Shannon -jj Behrens said...

> You can find a debian/Ubuntu-compliant init.d script for supervisor on my website.

Very helpful. Thanks!

Clint said...

> Huh? What does that have to do with launching a daemon on boot?

Ah right. Nothing to do with launching on boot, but ...

> I wanted something that could restart my process as necessary, so I decided against

... if your app responds properly to a appropriate SIG to restart, or roll your own signals for controlling the app. We did this with our web apps and decided on this code instead of Supvervisor due to the simplicity of it.

Graham Dumpleton said...

If this is for running Pylons, you also might look at running mod_wsgi under Apache. Given that Apache is usually integrated into startup scripts anyway, nothing would need to be done in respect to startup scripts.

If wanting an easy way to restart individual applications, then use mod_wsgi daemon mode and touching the WSGI application script file will cause a restart of that application on next request without needing to restart Apache. With daemon mode you can even run applications as specific users.

Shannon -jj Behrens said...

> If this is for running Pylons,...

Thanks for the advice. I decided to use Nginx and proxy to Paster. I'll keep mod_wsgi in mind, though.

chrism said...

I was hoping that Linux distributors would have packaged up supervisor by now for various platforms (and thus would have created rc.d scripts for it). The Fedora guys have done this already. It'd be nice to see the Ubuntu folks get it going.

The equivalent supervisor program config for this setup is something like:

[program:pylons]
user = www-data
directory = /var/run/myapp
command = paster serve production.ini

Wrt supervisor log rotation, maybe the problem is that it "just happens" without needing to set up an external log rotation program. Since supervisor captures the stdout (and stderr) of its children, it handles log rotation itself.

As long as the program you're running just sends stuff to stdout or stderr the following things are true:

- if the reason you want to set up log rotation is that you don't want your disk to fill up, but you don't really care too much about how long old logs are kept for, do nothing special to set up under supervisor. It will send log output to a temporary location.

- if you actually care about keeping backups of the log file and logging to a particular location, you need to set "stdout_logfile_maxbytes" and "stdout_logfile_backups" and perhaps "stdout_logfile" (to put the log in in a particular place) for the program you're running.

I don't know what Mike's problems with it were.

Shannon -jj Behrens said...

Chris, thanks for the comments ;)

BTW, in general, I was really impressed with Supervisor's documentation.

Дамјан Георгиевски said...

But Ubuntu (and Fedora 9 now) has upstart taht does the same thing as supervisor - right?

Shannon -jj Behrens said...

> But Ubuntu (and Fedora 9 now) has upstart taht does the same thing as supervisor - right?

Does upstart take care of restarting daemons and log rotation? It might. I don't know.