Zwillingssterns Weltenwald
Published on Zwillingssterns Weltenwald (http://www.xn--drachentrnen-ocb.de)

Startseite > Simple daemon with start-stop-daemon and runit

Simple daemon with start-stop-daemon and runit

PDF [1]

PDF [1] (to print)

Org [2] (source)

Creating a daemon with almost zero effort.

Table of Contents

  • start-stop-daemon
  • daemon with runit
    • runit as root
    • runit as simple user

start-stop-daemon

The example with the start-stop-daemon uses Gentoo OpenRC as root.

The simplest daemon we can create is a while loop:

echo '#!/bin/sh' > whiledaemon.sh
echo 'while true; do true; done' >> whiledaemon.sh
chmod +x whiledaemon.sh

Now we start it as daemon

start-stop-daemon --pidfile whiledaemon.pid \
--make-pidfile --background ./whiledaemon.sh

Top shows that it is running:

top | grep whiledaemon.sh

We stop it using the pidfile:

start-stop-daemon --pidfile whiledaemon.pid \
--stop ./whiledaemon.sh

That’s it.

Hint: To add cgroups support on a Gentoo [3] install, open /etc/rc.conf and uncomment

rc_controller_cgroups="YES"

Then in the initscript you can set the other variables described below that line. Thanks for this hint goes to Luca Barbato [4]!

If you want to ensure that the daemon keeps running without checking a PID file (which might in some corner cases fail because a new process claims the same PID), we can use runsvdir [5] from runit.

daemon with runit

Minimal examples for runit daemons - first as unpriviledged user, then as root.

runit as simple user

Create a script which dies

echo '#!/usr/bin/env python\nfor i in range(100): a = i*i' >/tmp/foo.py
chmod +x /tmp/foo.py

Create the daemon folder

mkdir -p ~/.local/run/runit_services/python
ln -sf /tmp/foo.py ~/.local/run/runit_services/python/run

Run the daemon via runsvdir

runsvdir ~/.local/run/runit_services

Manage it with sv (part of runit)

# stop the running daemon
SVDIR=~/.local/run/runit_services/ sv stop python
# start the service (it shows as `run` in top)
SVDIR=~/.local/run/runit_services/ sv start python

runit as root

Minimal working example for setting up runit as root - like a sysadmin might do it.

echo '#!/usr/bin/env python\nfor i in range(100): a = i*i' >/tmp/foo.py &&
    chmod +x /tmp/foo.py &&
    mkdir -p /run/arne_service/python &&
    printf '#!/bin/sh\nexec /tmp/foo.py' >/run/arne_service/python/run &&
    chmod +x /run/arne_service/python/run &&
    chown -R arne /run/arne_service &&
    su - arne -c 'runsvdir /run/arne_service'

Or without bash indirection (giving up some flexibility we don’t need here)

echo '#!/usr/bin/env python\nfor i in range(100): a = i*i' >/tmp/foo.py && 
    chmod +x /tmp/foo.py &&
    mkdir -p /run/arne_service/python &&
    ln -s /tmp/foo.py /run/arne_service/python/run &&
    chown -R arne /run/arne_service &&
    su - arne -c 'runsvdir /run/arne_service'
AnhangGröße
2015-04-15-Mi-simple-daemon-openrc.org [2]2.92 KB
2015-04-15-Mi-simple-daemon-openrc.pdf [1]152.99 KB
Werke von Arne Babenhauserheide. Lizensiert, wo nichts anderes steht, unter der GPLv3 or later und weiteren freien Lizenzen.

Diese Seite nutzt Cookies. Und Bilder. Manchmal auch Text. Eins davon muss ich wohl erwähnen — sagen die meisten anderen, und ich habe grade keine Zeit, Rechtstexte dazu zu lesen…


Source URL: http://www.xn--drachentrnen-ocb.de/english/free-software/start-stop-daemon-runit

Links:
[1] http://www.xn--drachentrnen-ocb.de/files/2015-04-15-Mi-simple-daemon-openrc_2.pdf
[2] http://www.xn--drachentrnen-ocb.de/files/2015-04-15-Mi-simple-daemon-openrc_2.org
[3] http://gentoo.org
[4] https://plus.google.com/u/0/+LucaBarbato/posts
[5] http://smarden.org/runit/runsvdir.8.html