blob: 52dd781efbed250ca0555fa0e77f908d5eaf0926 [file] [log] [blame]
Denys Vlasenkoee2d1942016-10-14 18:22:50 +02001 Daemontools and runit
Denys Vlasenko75bb3322010-12-06 15:13:58 +01002
Denys Vlasenkoee2d1942016-10-14 18:22:50 +02003Tired of PID files, needing root access, and writing init scripts just
4to have your UNIX apps start when your server boots? Want a simpler,
5better alternative that will also restart them if they crash? If so,
6this is an introduction to process supervision with runit/daemontools.
Denys Vlasenko75bb3322010-12-06 15:13:58 +01007
Denys Vlasenko75bb3322010-12-06 15:13:58 +01008
Denys Vlasenkoee2d1942016-10-14 18:22:50 +02009 Background
10
11Classic init scripts, e.g. /etc/init.d/apache, are widely used for
12starting processes at system boot time, when they are executed by init.
13Sadly, init scripts are cumbersome and error-prone to write, they must
14typically be edited and run as root, and the processes they launch do
15not get restarted automatically if they crash.
16
17In an alternative scheme called "process supervision", each important
18process is looked after by a tiny supervising process, which deals with
19starting and stopping the important process on request, and re-starting
20it when it exits unexpectedly. Those supervising processes can in turn
21be supervised by other supervising processes.
22
23Dan Bernstein wrote the process supervision toolkit, "daemontools",
24which is a set of small, reliable programs that cooperate in the
25UNIX tradition to manage process supervision trees.
26
27Runit is a more conveniently licensed and more actively maintained
28reimplementation of daemontools, written by Gerrit Pape.
29
30Here Ill use runit, however, the ideas are the same for other
31daemontools-like projects (there are several).
32
33
34 Service directories and scripts
35
36In runit parlance a "service" is simply a directory containing a script
37named "run".
38
39There are just two key programs in runit. Firstly, runsv supervises the
40process for an individual service. Service directories themselves sit
41inside a containing directory, and the runsvdir program supervises that
42directory, running one child runsv process for the service in each
43subdirectory. Out of the box on Debian, for example, an instance of
44runsvdir supervises services in subdirectories of /var/service/.
45
46If /var/service/log/ exists, runsv will supervise two services,
47and will connect stdout of main service to the stdin of log service.
48This is primarily used for logging.
49
50You can debug an individual service by running its SERVICE_DIR/run script.
Denys Vlasenko192c14b2014-02-21 12:55:43 +010051In this case, its stdout and stderr go to your terminal.
52
53You can also run "runsv SERVICE_DIR", which runs both the service
54and its logger service (SERVICE_DIR/log/run) if logger service exists.
55If logger service exists, the output will go to it instead of the terminal.
56
Denys Vlasenkoee2d1942016-10-14 18:22:50 +020057"runsvdir /var/service" merely runs "runsv SERVICE_DIR" for every subdirectory
58in /var/service.
Denys Vlasenko192c14b2014-02-21 12:55:43 +010059
Denys Vlasenko75bb3322010-12-06 15:13:58 +010060
Denys Vlasenkoee2d1942016-10-14 18:22:50 +020061 Examples
62
63This directory contains some examples of services:
64
65 var_service/getty_<tty>
66
67Runs a getty on <tty>. (run script looks at $PWD and extracts suffix
68after "_" as tty name). Create copies (or symlinks) of this directory
69with different names to run many gettys on many ttys.
70
71 var_service/gpm
72
73Runs gpm, the cut and paste utility and mouse server for text consoles.
74
75 var_service/inetd
76
77Runs inetd. This is an example of a service with log. Log service
78writes timestamped, rotated log data to /var/log/service/inetd/*
79using "svlogd -tt". p_log and w_log scripts demonstrage how you can
80"page log" and "watch log".
81
82Other services which have logs handle them in the same way.
83
84 var_service/nmeter
85
86Runs nmeter '%t %c ....' with output to /dev/tty9. This gives you
87a 1-second sampling of server load and health on a dedicated text console.
88
89
90 Networking examples
91
92In many cases, network configuration makes it necessary to run several daemons:
93dhcp, zeroconf, ppp, openvpn and such. They need to be controlled,
94and in many cases you also want to babysit them.
95
96They present a case where different services need to control (start, stop,
97restart) eact other.
98
99 var_service/dhcp_if
100
Denys Vlasenko75bb3322010-12-06 15:13:58 +0100101controls a udhcpc instance which provides dhpc-assigned IP
102address on interface named "if". Copy/rename this directory as needed to run
103udhcpc on other interfaces (var_service/dhcp_if/run script uses _foo suffix
Denys Vlasenkoee2d1942016-10-14 18:22:50 +0200104of the parent directory as interface name).
Denys Vlasenko75bb3322010-12-06 15:13:58 +0100105
Denys Vlasenkoee2d1942016-10-14 18:22:50 +0200106When IP address is obtained or lost, var_service/dhcp_if/dhcp_handler is run.
107It saves new config data to /var/run/service/fw/dhcp_if.ipconf and (re)starts
108/var/service/fw service. This example can be used as a template for other
109dynamic network link services (ppp/vpn/zcip).
110
111This is an example of service with has a "finish" script. If downed ("sv d"),
112"finish" is executed. For this service, it removes DHCP address from
113the interface.
114
115 var_service/zcip_if
116
117Zeroconf IP service: assigns a 169.254.x.y/16 address to interface "if".
118This allows to talk to other divices on a network without DHCP server
119(if they also assign 169.254 addresses to themselves).
120
121 var_service/ifplugd_if
122
123Watches link status of interface "if". Downs and ups /var/service/dhcp_if
Denys Vlasenko75bb3322010-12-06 15:13:58 +0100124service accordingly. In effect, it allows you to unplug/plug-to-different-network
125and have your IP properly re-negotiated at once.
126
Denys Vlasenkoee2d1942016-10-14 18:22:50 +0200127 var_service/dhcp_if_pinger
128
Denys Vlasenko1a1cfed2015-10-24 14:58:58 +0200129Uses var_service/dhcp_if's data to determine router IP. Pings it.
130If ping fails, restarts /var/service/dhcp_if service.
131Basically, an example of watchdog service for networks which are not reliable
132and need babysitting.
Denys Vlasenko75bb3322010-12-06 15:13:58 +0100133
Denys Vlasenkoee2d1942016-10-14 18:22:50 +0200134 var_service/supplicant_if
135
136Wireless supplicant (wifi association and encryption daemon) service for
137inteface "if".
138
139 var_service/fw
140
141This is an example of *one-shot* service.
142
143It reconfigures network based on current known state of ALL interfaces.
144Uses conf/*.ipconf (static config) and /var/run/service/fw/*.ipconf
Denys Vlasenko75bb3322010-12-06 15:13:58 +0100145(dynamic config from dhcp/ppp/vpn/etc) to determine what to do.
Denys Vlasenkoee2d1942016-10-14 18:22:50 +0200146
Denys Vlasenko75bb3322010-12-06 15:13:58 +0100147One-shot-ness of this service means that it shuts itself off after single run.
Denys Vlasenkobc3cdf82010-12-06 15:42:44 +0100148IOW: it is not a constantly running daemon sort of thing.
149It starts, it configures the network, it shuts down, all done
150(unlike infamous NetworkManagers which sit in RAM forever, doing hell knows what).
Denys Vlasenko75bb3322010-12-06 15:13:58 +0100151
152However, any dhcp/ppp/vpn or similar service can restart it anytime
153when it senses the change in network configuration.
154This even works while fw service runs: if dhcp signals fw to (re)start
155while fw runs, fw will not stop after its execution, but will re-execute once,
156picking up dhcp's new configuration.
157This is achieved very simply by having
Denys Vlasenko192c14b2014-02-21 12:55:43 +0100158 # Make ourself one-shot
159 sv o .
Denys Vlasenko75bb3322010-12-06 15:13:58 +0100160at the very beginning of fw/run script, not at the end.
161Therefore, any "sv u /var/run/service/fw" command by any other
162script "undoes" o(ne-shot) command if fw still runs, thus
163runsv will rerun it; or start it in a normal way if fw is not running.
164
165System administrators are expected to edit fw/run script, since
166network configuration needs are likely to be very complex and different
167for non-trivial installations.
Denys Vlasenkoee2d1942016-10-14 18:22:50 +0200168
169 var_service/ftpd
170 var_service/httpd
171 var_service/tftpd
172 var_service/ntpd
173
174Examples of typical network daemons.