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 Bukkit s…

Apple: iPad and Emacs

Someone asked my boss's buddy Art Medlar if he was going to buy an iPad. He said, "I figure as soon as it runs Emacs, that will be the sign to buy." I think he was just trying to be funny, but his statement is actually fairly profound.

It's well known that submitting iPhone and iPad applications for sale on Apple's store is a huge pain--even if they're free and open source. Apple is acting as a gatekeeper for what is and isn't allowed on your device. I heard that Apple would never allow a scripting language to be installed on your iPad because it would allow end users to run code that they hadn't verified. (I don't have a reference for this, but if you do, please post it below.) Emacs is mostly written in Emacs Lisp. Per Apple's policy, I don't think it'll ever be possible to run Emacs on the iPad.

Emacs was written by Richard Stallman, and it practically defines the Free Software movement (in a manner of speaking at least). Stal…

JavaScript: Porting from react-css-modules to babel-plugin-react-css-modules (with Less)

I recently found a bug in react-css-modules that prevented me from upgrading react-mobx which prevented us from upgrading to React 16. Then, I found out that react-css-modules is "no longer actively maintained". Hence, whether I wanted to or not, I was kind of forced into moving from react-css-modules to babel-plugin-react-css-modules. Doing the port is mostly straightforward. Once I switched libraries, the rest of the port was basically:
Get ESLint to pass now that react-css-modules is no longer available.Get babel-plugin-react-css-modules working with Less.Get my Karma tests to at least build.Get the Karma tests to pass.Test things thoroughly.Fight off merge conflicts from the rest of engineering every 10 minutes ;) There were a few things that resulted in difficult code changes. That's what the rest of this blog post is about. I don't think you can fix all of these things ahead of time. Just read through them and keep them in mind as you follow the approach above.…