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

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

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;) In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3 , Erlang , and the original Lisp machine . The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts. Now, I'm sure that many of these ideas have already been envisioned within Tunes.org , LLVM , Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway. Rather than wax philosophical, let me just dump out some ideas: Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code.&quo

Haskell or Erlang?

I've coded in both Erlang and Haskell. Erlang is practical, efficient, and useful. It's got a wonderful niche in the distributed world, and it has some real success stories such as CouchDB and jabber.org. Haskell is elegant and beautiful. It's been successful in various programming language competitions. I have some experience in both, but I'm thinking it's time to really commit to learning one of them on a professional level. They both have good books out now, and it's probably time I read one of those books cover to cover. My question is which? Back in 2000, Perl had established a real niche for systems administration, CGI, and text processing. The syntax wasn't exactly beautiful (unless you're into that sort of thing), but it was popular and mature. Python hadn't really become popular, nor did it really have a strong niche (at least as far as I could see). I went with Python because of its elegance, but since then, I've coded both p