Table of Contents
1 Regarding this document
- Whenever we talk about some arbitrary "service" we will use the example name
my_service. The common base directory for a service run throughrunitis then/etc/service/my_service/, e.g./etc/service/spark-masterholds the service which runs thespark-master.
2 TL;DR
Reference can be found here.
runit- a UNIX process no 1
runsv- starts and monitors a service and optionally an appendant log service
runsvdir- starts and monitors a collection of
runsv(8)processes sv- controls and manages services monitored by
runsv(8) svlogd- continuously reads log data from the service's
stdin, optionally filters log messages, and writes the data to one or more automatically rotated logs. chpst- runs a program with a changed process state
Processes/services are created as follows:
- Create a directory under
/etc/sv/, e.g./etc/sv/my_service/, and create a symbolic link from this dir to/etc/service/my_service/.- Service directory must be in
/etc/service/forrunsvto run it regularly and restart on shutdown, etc. - Any service directory added to
/etc/service/through creation or linking will be run byrunsvwithin 5 seconds - In fact, normally services run by
runsvwould be located at/service/but for some reason the image we use as a base in ourdockersetup uses/etc/service/so we're going with that for now.
- Service directory must be in
- Create a
runscript, e.g./etc/service/my_service/run- Simply a normal bash script or any executable
- Make sure it doesn't daemonize (i.e. runs in the background)
- Make sure it's executable (
chmodis your friend) - (Optional) Create a
finishscript which will run, well, when the service finishes, ergo whenrunprocess exits.- If
runorfinishexists right away,runsvwill wait for a bit before restarting the service - When
runORfinishfinishes, the service will be restarted- unless
my_service/downfile exists, in which case no restart will occur
- unless
Important: because
runsvwillstdoutto the logging-daemonsstdin, we need to pipestderrtostdoutin/etc/service/my_service/runto make sure we get all the output of the service.#!/bin/sh exec 2>&1 exec /usr/sbin/my_service
- If
- (Optional) Create a logging directory, to which
runsvwill pipe thestderrof the service, e.g./etc/service/my_service/log/- This directory follows almost all the same structure as the actual service dir,
- Requires a
runfile which will receive the service'sstderron itsstdin - Optionally can have a
finishexec and so on
- Requires a
- View logs and such using
svlogdas mentioned above A typical
my_service/log/runwill look something like this:#!/bin/sh exec svlogd -t /var/log/my_service/
where the
-targument is simply to tellsvlogdto add the timestamp at the beginning of each line.
- This directory follows almost all the same structure as the actual service dir,
- Control the process using
svcommand
3 Why runit ?
3.1 Service supervision
- Each service has its own designated service-directory
- Each daemon runs a child process of a supervising
runsvprocess running in the corresponding service-directory runsvcommand provides interface for signalling the service daemon and controlling the service and supervisor- Normally the
svcommand is used to send signals through this interface (runsv) and to query status information regarding the running service
3.2 Clean process state
runitguarantees each service a clean process state, no matter if the service is activated for the first time or automatically at boot time, reactivated, or simply restarted. Ergo, the service always started with the same:- environment
- resource limits
- open file descriptors
- controlling terminals
3.3 Reliable logging
runsvprovides a reliable logging facility for service daemon- If configured,
runsvdoes the following:- creates a pipe
- starts and supervises an additional log service
- redirects log daemon's
stdinto read from the pipe - service daemon's
stdoutto write to the pipe
- This means that restarting the actual service does not require a restart of the log daemon, and vice versa
- A very good choice for the log daemon is
runit's service logging daemonsvlogd
3.4 Fast boot and shutdown
- Services booted and shutdown in parallel
- On startup: happens after the system's one-time tasks (stage 1)
- On shutdown: stage 3 uses
runit's control interface to wait until each service daemon is shutdown and system halt/reboot is initiated right after
4 runsv - starter and monitor
runsv service
The main thing is that runsv switches to the service directory and runs ./run.
Further, it has a lot of conditional behavior:
4.1 Start and finish
./runfinishes and./finishexists \(\rightarrow\)runsvstarts./finish./finishdoesn't exist or finishes \(\rightarrow\)runsvrestarts the service./runor./finishexits immediately \(\rightarrow\)runsvwaits a second before restarting
4.2 Control
runsv can be controlled in two ways:
- use
sv(8)(recommended) - write command characters straight to
./supervise/control(and optionally./log/supervice/control)
For a full reference to the different controls, see here.
5 sv - controller and reporter
5.1 Source
5.2 Description
The sv program reports the current status and controls the state of services
monitored by the runsv(8) supervisor.
6 svlogd - log daemon
6.1 Source
6.2 Description
svlogdcontinuously reads log data from itsstdin, optionally filters log messages, and writes the data to one or more automatically rotated logs.- recent log-files can be processe automatically by an arbitrary program when they are rotated
svlogdcan be told to alert selected log messages tostderrand throughudpsvlogdruns until it sees EOF onstdinor sent aTERMsignal
6.3 Log-directory
The log-dir called log contains the following:
- some number of old log-files starting with
@, followed by a precise timestamp when the log-file was rotated out - current log-file with name
current - the lock-file
lock - maybe
statenewstate
- optional
config
See the source for more information regarding the config file and such.