Sunday, December 8, 2013

A primer on runit using Debian Wheezy - process supervision

Runit is a program used to make process supervision and as an alternative for startup scripts.
For the purpose of our example, we're are not going to use runit as a compliment to init.d.

Why should I use it?


It's easy to use.
It's multiplatform.
It allows to to avoid having to fight with pid files.
Helps you to avoid reinventing the wheel.


Installing runit

aptitude install runit
The installer will add the following two lines at the end of your /etc/inittab:
SV:123456:respawn:/usr/sbin/runsvdir-start
SV:123456:respawn:/sbin/runsvdir-start
After installing, you'll have to options to start using runit: reboot o reread your /etc/inittab file by running:
init q
From now on, every time we start your OS it will run the runsvdir daemon. This process will be responsible for monitoring the /etc/service/ directory where we will configure our services. By each service running on this directory, the runsvdir daemon will spawn a new runsv process.

Configuring our first service.

Create the service directory
mkdir -p /etc/sv/test
Create the service script and the script to log everything that happens with the service:
touch /etc/sv/test/run /etc/sv/test/log/run
Give running persmission. This is very important. Otherwise the services won't run:
chmod u+x /etc/sv/test/run
Our really simple 'service' that writes to stdout and stderr every and die after 5 seconds:
#!/bin/sh
exec 2>&1
exec bash - <
Note: We need to use exec in order to make our command replace the current shell without creating a new process. Content of /etc/sv/test/log/run:
#!/bin/sh

exec chpst -u nobody svlogd -ttt /var/log/test/

We have our services configured but not under supervision. We need to create a symbolic link to the /etc/service/ directory:
pushd /etc/service/
ls -s ../sv/test/
popd

Show time

Everything its setup. Let's do some test:
# start the service
root@beta:/etc/service# sv start test
ok: run: test: (pid 26967) 0s

# lets check our service during 5 seconds to see how it behaves
root@beta:/etc/service# sv status test
run: test: (pid 26972) 0s; run: log: (pid 2643) 25259s
root@beta:/etc/service# sv status test
run: test: (pid 26972) 1s; run: log: (pid 2643) 25260s
root@beta:/etc/service# sv status test
run: test: (pid 26972) 2s; run: log: (pid 2643) 25261s
root@beta:/etc/service# sv status test
run: test: (pid 26972) 3s; run: log: (pid 2643) 25262s
root@beta:/etc/service# sv status test
run: test: (pid 26972) 4s; run: log: (pid 2643) 25263s
root@beta:/etc/service# sv status test
run: test: (pid 26972) 5s; run: log: (pid 2643) 25264s
root@beta:/etc/service# sv status test
run: test: (pid 26984) 0s; run: log: (pid 2643) 25264s
# the service got restarted automatically!!
root@beta:/etc/service# sv status test
down: test: 5s, normally up; run: log: (pid 2643) 25276s

# now we can stop out service
root@beta:/etc/service# sv stop test
ok: down: test: 1s, normally up

This is it for this first post on runit.

1 comment:

  1. Hello,
    any ideas how to completly remove systemd and replace it with runit?

    ReplyDelete