README 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. Daemontools and runit
  2. Tired of PID files, needing root access, and writing init scripts just
  3. to have your UNIX apps start when your server boots? Want a simpler,
  4. better alternative that will also restart them if they crash? If so,
  5. this is an introduction to process supervision with runit/daemontools.
  6. Background
  7. Classic init scripts, e.g. /etc/init.d/apache, are widely used for
  8. starting processes at system boot time, when they are executed by init.
  9. Sadly, init scripts are cumbersome and error-prone to write, they must
  10. typically be edited and run as root, and the processes they launch do
  11. not get restarted automatically if they crash.
  12. In an alternative scheme called "process supervision", each important
  13. process is looked after by a tiny supervising process, which deals with
  14. starting and stopping the important process on request, and re-starting
  15. it when it exits unexpectedly. Those supervising processes can in turn
  16. be supervised by other supervising processes.
  17. Dan Bernstein wrote the process supervision toolkit, "daemontools",
  18. which is a set of small, reliable programs that cooperate in the
  19. UNIX tradition to manage process supervision trees.
  20. Runit is a more conveniently licensed and more actively maintained
  21. reimplementation of daemontools, written by Gerrit Pape.
  22. Here I’ll use runit, however, the ideas are the same for other
  23. daemontools-like projects (there are several).
  24. Service directories and scripts
  25. In runit parlance a "service" is simply a directory containing a script
  26. named "run".
  27. There are just two key programs in runit. Firstly, runsv supervises the
  28. process for an individual service. Service directories themselves sit
  29. inside a containing directory, and the runsvdir program supervises that
  30. directory, running one child runsv process for the service in each
  31. subdirectory. A typical choice is to start an instance of runsvdir
  32. which supervises services in subdirectories of /var/service/.
  33. If /var/service/log/ exists, runsv will supervise two services,
  34. and will connect stdout of main service to the stdin of log service.
  35. This is primarily used for logging.
  36. You can debug an individual service by running its SERVICE_DIR/run script.
  37. In this case, its stdout and stderr go to your terminal.
  38. You can also run "runsv SERVICE_DIR", which runs both the service
  39. and its logger service (SERVICE_DIR/log/run) if logger service exists.
  40. If logger service exists, the output will go to it instead of the terminal.
  41. "runsvdir /var/service" merely runs "runsv SERVICE_DIR" for every subdirectory
  42. in /var/service.
  43. Examples
  44. This directory contains some examples of services:
  45. var_service/getty_<tty>
  46. Runs a getty on <tty>. (run script looks at $PWD and extracts suffix
  47. after "_" as tty name). Create copies (or symlinks) of this directory
  48. with different names to run many gettys on many ttys.
  49. var_service/gpm
  50. Runs gpm, the cut and paste utility and mouse server for text consoles.
  51. var_service/inetd
  52. Runs inetd. This is an example of a service with log. Log service
  53. writes timestamped, rotated log data to /var/log/service/inetd/*
  54. using "svlogd -tt". p_log and w_log scripts demonstrage how you can
  55. "page log" and "watch log".
  56. Other services which have logs handle them in the same way.
  57. var_service/nmeter
  58. Runs nmeter '%t %c ....' with output to /dev/tty9. This gives you
  59. a 1-second sampling of server load and health on a dedicated text console.
  60. Networking examples
  61. In many cases, network configuration makes it necessary to run several daemons:
  62. dhcp, zeroconf, ppp, openvpn and such. They need to be controlled,
  63. and in many cases you also want to babysit them.
  64. They present a case where different services need to control (start, stop,
  65. restart) each other.
  66. var_service/dhcp_if
  67. controls a udhcpc instance which provides DHCP-assigned IP
  68. address on interface named "if". Copy/rename this directory as needed to run
  69. udhcpc on other interfaces (var_service/dhcp_if/run script uses _foo suffix
  70. of the parent directory as interface name).
  71. When IP address is obtained or lost, var_service/dhcp_if/dhcp_handler is run.
  72. It saves new config data to /var/run/service/fw/dhcp_if.ipconf and (re)starts
  73. /var/service/fw service. This example can be used as a template for other
  74. dynamic network link services (ppp/vpn/zcip).
  75. This is an example of service with has a "finish" script. If downed ("sv d"),
  76. "finish" is executed. For this service, it removes DHCP address from
  77. the interface. This is useful when ifplugd detects that the the link is dead
  78. (cable is no longer attached anywhere) and downs us - keeping DHCP configured
  79. addresses on the interface would make kernel still try to use it.
  80. var_service/zcip_if
  81. Zeroconf IP service: assigns a 169.254.x.y/16 address to interface "if".
  82. This allows to talk to other devices on a network without DHCP server
  83. (if they also assign 169.254 addresses to themselves).
  84. var_service/ifplugd_if
  85. Watches link status of interface "if". Downs and ups /var/service/dhcp_if
  86. service accordingly. In effect, it allows you to unplug/plug-to-different-network
  87. and have your IP properly re-negotiated at once.
  88. var_service/dhcp_if_pinger
  89. Uses var_service/dhcp_if's data to determine router IP. Pings it.
  90. If ping fails, restarts /var/service/dhcp_if service.
  91. Basically, an example of watchdog service for networks which are not reliable
  92. and need babysitting.
  93. var_service/supplicant_if
  94. Wireless supplicant (wifi association and encryption daemon) service for
  95. interface "if".
  96. var_service/fw
  97. "Firewall" script, although it is tasked with much more than setting up firewall.
  98. It is responsible for all aspects of network configuration.
  99. This is an example of *one-shot* service.
  100. It reconfigures network based on current known state of ALL interfaces.
  101. Uses conf/*.ipconf (static config) and /var/run/service/fw/*.ipconf
  102. (dynamic config from dhcp/ppp/vpn/etc) to determine what to do.
  103. One-shot-ness of this service means that it shuts itself off after single run.
  104. IOW: it is not a constantly running daemon sort of thing.
  105. It starts, it configures the network, it shuts down, all done
  106. (unlike infamous NetworkManagers which sit in RAM forever).
  107. However, any dhcp/ppp/vpn or similar service can restart it anytime
  108. when it senses the change in network configuration.
  109. This even works while fw service runs: if dhcp signals fw to (re)start
  110. while fw runs, fw will not stop after its execution, but will re-execute once,
  111. picking up dhcp's new configuration.
  112. This is achieved very simply by having
  113. # Make ourself one-shot
  114. sv o .
  115. at the very beginning of fw/run script, not at the end.
  116. Therefore, any "sv u fw" command by any other script "undoes" o(ne-shot)
  117. command if fw still runs, thus runsv will rerun it; or start it
  118. in a normal way if fw is not running.
  119. This mechanism is the reason why fw is a service, not just a script.
  120. System administrators are expected to edit fw/run script, since
  121. network configuration needs are likely to be very complex and different
  122. for non-trivial installations.
  123. var_service/ftpd
  124. var_service/httpd
  125. var_service/tftpd
  126. var_service/ntpd
  127. Examples of typical network daemons.
  128. Process tree
  129. Here is an example of the process tree from a live system with these services
  130. (and a few others). An interesting detail are ftpd and vpnc services, where
  131. you can see only logger process. These services are "downed" at the moment:
  132. their daemons are not launched.
  133. PID TIME COMMAND
  134. 553 0:04 runsvdir -P /var/service
  135. 561 0:00 runsv sshd
  136. 576 0:00 svlogd -tt /var/log/service/sshd
  137. 589 0:00 /usr/sbin/sshd -D -e -p22 -u0 -h /var/service/sshd/ssh_host_rsa_key
  138. 562 0:00 runsv dhcp_eth0
  139. 568 0:00 svlogd -tt /var/log/service/dhcp_eth0
  140. 850 0:00 udhcpc -vv --foreground --interface=eth0
  141. --pidfile=/var/service/dhcp_eth0/udhcpc.pid
  142. --script=/var/service/dhcp_eth0/dhcp_handler
  143. -x hostname bbox
  144. 563 0:00 runsv ntpd
  145. 573 0:01 svlogd -tt /var/log/service/ntpd
  146. 845 0:00 busybox ntpd -dddnNl -S ./ntp.script -p 10.x.x.x -p 10.x.x.x
  147. 564 0:00 runsv ifplugd_wlan0
  148. 598 0:00 svlogd -tt /var/log/service/ifplugd_wlan0
  149. 614 0:05 ifplugd -apqns -t3 -u0 -d0 -i wlan0
  150. -r /var/service/ifplugd_wlan0/ifplugd_handler
  151. 565 0:08 runsv dhcp_wlan0_pinger
  152. 911 0:00 sleep 67
  153. 566 0:00 runsv unscd
  154. 583 0:03 svlogd -tt /var/log/service/unscd
  155. 599 0:02 nscd -dddd
  156. 567 0:00 runsv dhcp_wlan0
  157. 591 0:00 svlogd -tt /var/log/service/dhcp_wlan0
  158. 802 0:00 udhcpc -vv -C -o -V --foreground --interface=wlan0
  159. --pidfile=/var/service/dhcp_wlan0/udhcpc.pid
  160. --script=/var/service/dhcp_wlan0/dhcp_handler
  161. 569 0:00 runsv fw
  162. 570 0:00 runsv ifplugd_eth0
  163. 597 0:00 svlogd -tt /var/log/service/ifplugd_eth0
  164. 612 0:05 ifplugd -apqns -t3 -u8 -d8 -i eth0
  165. -r /var/service/ifplugd_eth0/ifplugd_handler
  166. 571 0:00 runsv zcip_eth0
  167. 590 0:00 svlogd -tt /var/log/service/zcip_eth0
  168. 607 0:01 zcip -fvv eth0 /var/service/zcip_eth0/zcip_handler
  169. 572 0:00 runsv ftpd
  170. 604 0:00 svlogd -tt /var/log/service/ftpd
  171. 574 0:00 runsv vpnc
  172. 603 0:00 svlogd -tt /var/log/service/vpnc
  173. 575 0:00 runsv httpd
  174. 602 0:00 svlogd -tt /var/log/service/httpd
  175. 622 0:00 busybox httpd -p80 -vvv -f -h /home/httpd_root
  176. 577 0:00 runsv supplicant_wlan0
  177. 627 0:00 svlogd -tt /var/log/service/supplicant_wlan0
  178. 638 0:03 wpa_supplicant -i wlan0
  179. -c /var/service/supplicant_wlan0/wpa_supplicant.conf -d