Skip to main content

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.

Comments

jjinux 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
jjinux 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__
Anonymous 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"])
jjinux 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.
jjinux said…
> You can find a debian/Ubuntu-compliant init.d script for supervisor on my website.

Very helpful. Thanks!
Anonymous 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.
Unknown 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.
jjinux 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.
Anonymous 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.
jjinux 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?
jjinux 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.

Popular posts from this blog

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python . Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening. The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for B

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

Creating Windows 10 Boot Media for a Lenovo Thinkpad T410 Using Only a Mac and a Linux Machine

TL;DR: Giovanni and I struggled trying to get Windows 10 installed on the Lenovo Thinkpad T410. We struggled a lot trying to create the installation media because we only had a Mac and a Linux machine to work with. Everytime we tried to boot the USB thumb drive, it just showed us a blinking cursor. At the end, we finally realized that Windows 10 wasn't supported on this laptop :-/ I've heard that it took Thomas Edison 100 tries to figure out the right material to use as a lightbulb filament. Well, I'm no Thomas Edison, but I thought it might be noteworthy to document our attempts at getting it to boot off a USB thumb drive: Download the ISO. Attempt 1: Use Etcher. Etcher says it doesn't work for Windows. Attempt 2: Use Boot Camp Assistant. It doesn't have that feature anymore. Attempt 3: Use Disk Utility on a Mac. Erase a USB thumb drive: Format: ExFAT Scheme: GUID Partition Map Mount the ISO. Copy everything from