| 1 | Service Options |
| 2 | =============== |
| 3 | |
| 4 | The run/task/tty/service/sysv directives take modifiers, or options, to |
| 5 | control their behavior. This section lists them with their limitations. |
| 6 | All modifiers must be placed between the directive and its command. |
| 7 | |
| 8 | The name of a service, shown by the `initctl` tool, defaults to the |
| 9 | basename of the service executable. It can be changed with the `name:` |
| 10 | option: |
| 11 | |
| 12 | name:<service-name> |
| 13 | |
| 14 | For multiple instances of a service, with the same `name`, set the |
| 15 | identifier `:ID` to prevent Finit from replacing previous instances: |
| 16 | |
| 17 | service name:ssdp :eth1 ssdpd eth1 -- Windows discovery on eth1 |
| 18 | service name:ssdp :eth2 ssdpd eth2 -- Windows discovery on eth2 |
| 19 | |
| 20 | The [`initctl`](../initctl.md) tool will list these two services as: |
| 21 | |
| 22 | - ssdp:eth1 |
| 23 | - ssdp:eth2 |
| 24 | |
| 25 | Conflicting services that must be prevented from starting, use the |
| 26 | `conflict:` option: |
| 27 | |
| 28 | service [S12345789] udevd -- Device event management daemon |
| 29 | run [S] conflict:udevd mdev -s -- Populating device tree |
| 30 | |
| 31 | Multiple conflicting services can be separated using `,`: |
| 32 | |
| 33 | service :1 abc |
| 34 | service :2 abc |
| 35 | service conflict:abc:1,abc:2 cde |
| 36 | |
| 37 | If a service should not be automatically started, it can be configured |
| 38 | as manual with the optional `manual` argument. The service can then be |
| 39 | started at any time by running `initctl start <service>`. |
| 40 | |
| 41 | manual:yes |
| 42 | |
| 43 | Other run/task/service options are: |
| 44 | |
| 45 | * `cgroups:...` -- see the [Cgroups](cgroups.md) section |
| 46 | * `env:[-]/path/to/env` -- see the [Service Environment](service-env.md) section |
| 47 | * `log:...` -- see [Redirecting Output](logging.md#redirecting-output) |
| 48 | * `nowarn` -- see [Conditional Loading](services.md#conditional-loading) |
| 49 | * `notify:...` -- see [Service Synchronization](service-sync.md) |
| 50 | * `if:...` -- see [Conditional Execution](services.md#conditional-execution) |
| 51 | * `type:forking` -- see description of the [service](services.md) directive |
| 52 | |
| 53 | As mentioned previously, services are automatically started, restarted, |
| 54 | and stopped, depending on the configuration and conditions. Within the |
| 55 | confines of that the following options are available: |
| 56 | |
| 57 | * `restart:NUM` -- number of times Finit tries to restart a crashing |
| 58 | service, default: 10, max: 255. When this limit is reached the |
| 59 | service is marked *crashed* and must be restarted manually with |
| 60 | `initctl restart NAME` |
| 61 | * `restart_sec:SEC` -- number of seconds before Finit tries to restart |
| 62 | a crashing service, default: 2 seconds for the first five retries, |
| 63 | then back-off to 5 seconds. The maximum of this configured value |
| 64 | and the above (2 and 5) will be used |
| 65 | * `restart:always` -- no upper limit on the number of times Finit |
| 66 | tries to restart a crashing service. Same as `restart:-1` |
| 67 | * `norestart` -- dont restart on failures, same as `restart:0` |
| 68 | * `respawn` -- bypasses the `restart` mechanism completely, allows |
| 69 | endless restarts. Useful in many use-cases, but not what `service` |
| 70 | was originally designed for so not the default behavior |
| 71 | * `oncrash:reboot` -- when all retries have failed, and the service |
| 72 | has *crashed*, if this option is set the system is rebooted |
| 73 | * `oncrash:script` -- similarly, but instead of rebooting, call the |
| 74 | `post:script` action with exit code `crashed`, see below |
| 75 | * `reload:'script [args]'` -- some services do not support `SIGHUP` but |
| 76 | may have other ways to update the configuration of a running daemon. |
| 77 | When `reload:script` is defined it is preferred over `SIGHUP`. Like |
| 78 | systemd, Finit sets `$MAINPID` as a convenience to scripts, which in |
| 79 | effect also allow `reload:'kill -HUP $MAINPID'` |
| 80 | * `stop:'script [args]'` -- some services may require alternate methods |
| 81 | to be stopped. If a `stop:script` is defined it is preferred over |
| 82 | `SIGTERM` and `stop`, for `service` and `sysv`, respectively. |
| 83 | Similar to `reload:script`, Finit sets `$MAINPID` |
| 84 | |
| 85 | > [!CAUTION] |
| 86 | > Both `reload:script` and `stop:script` are called as PID 1, without |
| 87 | > any timeout! Meaning, it is up to you to ensure the script is not |
| 88 | > blocking for seconds at a time or never terminates. |
| 89 | |
| 90 | When stopping a service (run/task/sysv/service), either manually or when |
| 91 | moving to another runlevel, Finit starts by sending `SIGTERM`, to allow |
| 92 | the process to shut down gracefully (unless a `stop:'script'` is used). |
| 93 | However, if the process has not been collected within 3 seconds, Finit |
| 94 | will send `SIGKILL`. To stop the process using a different signal than |
| 95 | `SIGTERM`, use `halt:SIGNAL`, e.g., `halt:SIGPWR`. To change the delay |
| 96 | between the stop signal and KILL, use the option `kill:<1-60>`, e.g., |
| 97 | `kill:10` to wait 10 seconds before sending `SIGKILL`. |
| 98 | |
| 99 | Services, including the `sysv` variant, support pre/post/ready and |
| 100 | cleanup scripts: |
| 101 | |
| 102 | * `pre:[0-3600,]script` -- called before the sysv/service is stated |
| 103 | * `post:[0-3600,]script` -- called after the sysv/service has stopped |
| 104 | * `ready:[0-3600,]script` -- called when the sysv/service is ready |
| 105 | * `cleanup:[0-3600,]script` -- called when run/task/sysv/service is removed |
| 106 | |
| 107 | The optional number (0-3600) is the timeout before Finit kills the |
| 108 | script, it defaults to the kill delay value and can be disabled by |
| 109 | setting it to zero. These scripts run as the same `@USER:GROUP` as the |
| 110 | service itself, with any `env:file` sourced. The scripts are executed |
| 111 | from the `$HOME` of the given user. The scripts are not called with any |
| 112 | argument, but get a set of environment variables: |
| 113 | |
| 114 | * `SERVICE_IDENT=foo:1` |
| 115 | * `SERVICE_NAME=foo` |
| 116 | * `SERVICE_ID=1` |
| 117 | |
| 118 | The `post:script` is called with an additional set of environment |
| 119 | variables. Yes, the text is correct, the naming was an accident: |
| 120 | |
| 121 | - `EXIT_CODE=[exited,signal,crashed]`: normal exit, signaled, or |
| 122 | crashed |
| 123 | - `EXIT_STATUS=[num,SIGNAME]`: set to one of exit status code from |
| 124 | the program, if it exited normally, or the signal name (`HUP`, |
| 125 | `TERM`, etc.) if it exited due to signal |
| 126 | |
| 127 | When a run/task/sys/service is removed (disable + reload) it is first |
| 128 | stopped and then removed from the runlevel. The `post:script` always |
| 129 | runs when the process has stopped, and the `cleanup:script` runs when |
| 130 | the the stanza has been removed from the runlevel. |
| 131 | |
| 132 | > [!IMPORTANT] |
| 133 | > These script actions are intended for setup, cleanup, and readiness |
| 134 | > notification. It is up to the user to ensure the scripts terminate. |