| 1 | Services |
| 2 | ======== |
| 3 | |
| 4 | **Syntax:** `service [LVLS] <COND> /path/to/daemon ARGS -- Optional description` |
| 5 | |
| 6 | Service, or daemon, to be monitored and automatically restarted if it |
| 7 | exits prematurely. Finit tries to restart services that die, by default |
| 8 | 10 times before giving up and marking them as *crashed*. After which |
| 9 | they have to be manually restarted with `initctl restart NAME`. The |
| 10 | limits controlling this are configurable, see the options below. |
| 11 | |
| 12 | > [!TIP] |
| 13 | > To allow endless restarts, see the [`respawn` option](service-opts.md) |
| 14 | |
| 15 | For daemons that support it, we recommend appending `--foreground`, |
| 16 | `--no-background`, `-n`, `-F`, or similar command line argument to |
| 17 | prevent them from forking off a sub-process in the background. This is |
| 18 | the most reliable way to monitor a service. |
| 19 | |
| 20 | However, not all daemons support running in the foreground, or they may |
| 21 | start logging to the foreground as well, these are forking daemons and |
| 22 | are supported using the same syntax as forking `sysv` services, using |
| 23 | the `pid:!/path/to/pidfile.pid` syntax. There is an alternative syntax |
| 24 | that may be more intuitive, where Finit can also guess the PID file |
| 25 | based on the daemon's command name: |
| 26 | |
| 27 | service type:forking ntpd -- NTP daemon |
| 28 | |
| 29 | This example lets BusyBox `ntpd` daemonize itself. Finit uses the |
| 30 | basename of the binary to guess the PID file to watch for the PID: |
| 31 | `/var/run/ntpd.pid`. If Finit guesses wrong, you have to submit the |
| 32 | full `pid:!/path/to/file.pid`. |
| 33 | |
| 34 | **Example:** |
| 35 | |
| 36 | In the case of `ospfd` (below), we omit the `-d` flag (daemonize) to |
| 37 | prevent it from forking to the background: |
| 38 | |
| 39 | service [2345] <pid/zebra> /sbin/ospfd -- OSPF daemon |
| 40 | |
| 41 | `[2345]` denote the runlevels `ospfd` is allowed to run in, they are |
| 42 | optional and default to level 2-5 if omitted. |
| 43 | |
| 44 | `<...>` is the condition for starting `ospfd`. In this example Finit |
| 45 | waits for another service, `zebra`, to have created its PID file in |
| 46 | `/var/run/quagga/zebra.pid` before starting `ospfd`. Finit watches |
| 47 | *all* files in `/var/run`, for each file named `*.pid`, or `*/pid`, |
| 48 | Finit opens it and find the matching `NAME:ID` using the PID. |
| 49 | |
| 50 | Some services do not maintain a PID file and rather than patching each |
| 51 | application Finit provides a workaround. A `pid` keyword can be set |
| 52 | to have Finit automatically create (when starting) and later remove |
| 53 | (when stopping) the PID file. The file is created in the `/var/run` |
| 54 | directory using the `basename(1)` of the service. The default can be |
| 55 | modified with an optional `pid:`-argument: |
| 56 | |
| 57 | pid[:[/path/to/]filename[.pid]] |
| 58 | |
| 59 | For example, by adding `pid:/run/bar.pid` to the service `/sbin/bar`, |
| 60 | that PID file will, not only be created and removed automatically, but |
| 61 | also be used by the Finit condition subsystem. So a service/run/task |
| 62 | can depend on `<pid/bar>`, like this foo will not be started until bar |
| 63 | has started: |
| 64 | |
| 65 | service pid:/run/bar.pid bar -- Bar Service |
| 66 | service <pid/bar> foo -- Foo Service |
| 67 | |
| 68 | Needless to say, it is better if `bar` creates its own PID file when it |
| 69 | has completed starting up and is ready for service. |
| 70 | |
| 71 | As an alternative "readiness" notification, Finit supports both systemd |
| 72 | and s6 style notification. This can be enabled by using the `notify` |
| 73 | option: |
| 74 | |
| 75 | * `notify:systemd` -- tells Finit the service uses the `sd_notify()` |
| 76 | API to signal PID 1 when it has completed its startup and is ready |
| 77 | to service events. The [sd_notify()][] API expects `NOTIFY_SOCKET` |
| 78 | to be set to the socket where the application can send `"READY=1\n"` |
| 79 | when it is starting up or has processed a `SIGHUP`. |
| 80 | * `notify:s6` -- puts Finit in s6 compatibility mode. Compared to the |
| 81 | systemd notification, [s6 expect][] compliant daemons to send `"\n"` |
| 82 | and then close their socket. Finit takes care of "hard-wiring" the |
| 83 | READY state as long as the application is running, events across any |
| 84 | `SIGHUP`. Since s6 can give its applications the descriptor number |
| 85 | (must be >3) on then command line, Finit provides the following |
| 86 | syntax (`%n` is replaced by Finit with then descriptor number): |
| 87 | |
| 88 | service [S12345789] notify:s6 mdevd -O 4 -D %n |
| 89 | |
| 90 | [sd_notify()]: https://www.freedesktop.org/software/systemd/man/sd_notify.html |
| 91 | [s6 expect]: https://skarnet.org/software/s6/notifywhenup.html |
| 92 | |
| 93 | When a service is ready, either by Finit detecting its PID file, or |
| 94 | their respective readiness mechanism has been triggered, Finit creates |
| 95 | then service's ready condition which other services can depend on: |
| 96 | |
| 97 | $ initctl -v cond get service/mdevd/ready |
| 98 | on |
| 99 | |
| 100 | This can be used to synchronize the start of another run/task/service: |
| 101 | |
| 102 | task [S] <service/mdevd/ready> @root:root mdevd-coldplug |
| 103 | |
| 104 | Finit waits for `mdevd` to notify it, before starting `mdevd-coldplug`. |
| 105 | Notice how both start in runlevel S, and the coldplug task only runs in |
| 106 | S. When the system moves to runlevel 2 (the default), coldplug is no |
| 107 | longer part of the running configuration (`initctl show`), this is to |
| 108 | ensure that coldplug is not called more than once. |
| 109 | |
| 110 | > For a detailed description of conditions, and how to debug them, |
| 111 | > see the [Finit Conditions](../conditions.md) document. |
| 112 | |
| 113 | |
| 114 | Non-privileged Services |
| 115 | ----------------------- |
| 116 | |
| 117 | Every `run`, `task`, or `service` can also list the privileges the |
| 118 | `/path/to/cmd` should be executed with. Prefix the command with |
| 119 | `@USR[:GRP]`, group is optional, like this: |
| 120 | |
| 121 | run [2345] @joe:users logger "Hello world" |
| 122 | |
| 123 | For multiple instances of the same command, e.g. a DHCP client or |
| 124 | multiple web servers, add `:ID` somewhere between the `run`, `task`, |
| 125 | `service` keyword and the command, like this: |
| 126 | |
| 127 | service :80 [2345] httpd -f -h /http -p 80 -- Web server |
| 128 | service :8080[2345] httpd -f -h /http -p 8080 -- Old web server |
| 129 | |
| 130 | Without the `:ID` to the service the latter will overwrite the former |
| 131 | and only the old web server would be started and supervised. |
| 132 | |
| 133 | |
| 134 | Conditional Loading |
| 135 | ------------------- |
| 136 | |
| 137 | Finit support conditional loading of stanzas. The following example is |
| 138 | take from the `system/hotplug.conf` file in the Finit distribution. |
| 139 | Here we only show a simplified subset. |
| 140 | |
| 141 | Starting with the `nowarn` option. |
| 142 | |
| 143 | service nowarn name:udevd pid:udevd /lib/systemd/systemd-udevd |
| 144 | service nowarn name:udevd pid:udevd udevd |
| 145 | |
| 146 | When loading the .conf file Finit looks for `/lib/systemd/systemd-udevd` |
| 147 | if that is not found Finit automatically logs a warning. The `nowarn` |
| 148 | option disables this warning so that the second line can be evaluated, |
| 149 | which also provides a service named `udevd`. |
| 150 | |
| 151 | run nowarn if:udevd <pid/udevd> :1 udevadm settle -t 0 |
| 152 | |
| 153 | This line is only loaded if we know of a service named `udevd`. Again, |
| 154 | we do not warn if `udevadm` is not found, execution will also stop here |
| 155 | until the PID condition is asserted, i.e., Finit detecting udevd has |
| 156 | started. |
| 157 | |
| 158 | run nowarn conflict:udevd [S] mdev -s -- Populating device tree |
| 159 | |
| 160 | If `udevd` is not available, we try to run `mdev`, but if that is not |
| 161 | found, again we do not warn. |
| 162 | |
| 163 | Conditional loading statements can also be negated, so the previous stanza |
| 164 | can also be written as: |
| 165 | |
| 166 | run nowarn if:!udevd [S] mdev -s -- Populating device tree |
| 167 | |
| 168 | The reason for using `conflict` in this example is that a conflict can be |
| 169 | resolved. Stanzas marked with `conflict:foo` are rechecked at runtime. |
| 170 | |
| 171 | |
| 172 | Conditional Execution |
| 173 | --------------------- |
| 174 | |
| 175 | Similar to conditional loading of stanzas there is conditional runtime |
| 176 | execution. This can be confusing at first, since Finit already has a |
| 177 | condition subsystem, but this is more akin to the qualification to a |
| 178 | runlevel. E.g., a `task [123]` is qualified to run only in runlevel 1, |
| 179 | 2, and 3. It is not considered for other runlevels. |
| 180 | |
| 181 | Conditional execution qualify a run/task/service based on a condition. |
| 182 | Consider this (simplified) example from the Infix operating system: |
| 183 | |
| 184 | run [S] name:startup <pid/sysrepo> confd -b --load startup-config |
| 185 | run [S] if:<usr/fail-startup> name:failure <pid/sysrepo> confd --load failure-config |
| 186 | |
| 187 | The two run statements reside in the same .conf file so Finit runs them |
| 188 | in true sequence. If loading the file `startup-config` fails confd sets |
| 189 | the condition `usr/fail-startup`, thus allowing the next run statement |
| 190 | to load `failure-config`. |
| 191 | |
| 192 | Notice the critical difference between the `<pid/sysrepo>` condition and |
| 193 | `if:<usr/fail-startup>`. The former is a condition for starting and the |
| 194 | latter is a condition to check if a run/task/service is qualified to |
| 195 | even be considered. |
| 196 | |
| 197 | Conditional execution statements can also be negated, so provided the |
| 198 | file loaded did the opposite, i.e., set a condition on success, the |
| 199 | previous stanza can also be written as: |
| 200 | |
| 201 | run [S] if:<!usr/startup-ok> name:failure <pid/sysrepo> confd ... |