diff --git a/README.md b/README.md index ad49294..e4990aa 100644 --- a/README.md +++ b/README.md @@ -1,47 +1,211 @@ -ssm -=== +# ssm Yet another service manager. -Services --------- -A service is a script in the ssm's init.d directory. -By default it's /etc/ssm/services for system services and $XDG_CONFIG_HOME/ssm/services for user ones. +Services are BASH scripts that get *sourced* by `ssm`. Same pitfalls apply — +keep complex logic out of them. -Note that they are BASH scripts that get sourced by `ssm`. Same pitfalls apply. -Hopefully, you won't have to do complex logic in the services. +## Install -## Global settings +Copy `ssm` somewhere on your `PATH` (e.g. `/usr/local/bin/ssm`) and make it +executable. Services live in a directory tree, searched in order (first hit wins): -* `cgroups = 0` — Enable cgroup-related features. +* `$rundir/current` — a copy of the configs ssm has remembered, see below +* `$XDG_CONFIG_HOME/ssm/services` (user) / `/etc/ssm/services` (system) +* `/usr/share/ssm/services` -## Service config +`$XDG_CONFIG_HOME` defaults to `~/.config`. System-wide config is read from +`/etc/ssm/ssm.conf`, user-wide from `$XDG_CONFIG_HOME/ssm/ssm.conf`. +The same two directories hold common `functions/` snippets that are sourced +before any service config is read. -Mandatory options: +## Usage -* `service_command = /usr/bin/example -a --long-arg "parameter"` +``` +ssm +``` -Optional settings (incomplete list): +Actions (or service-provided functions): -* `service_respawn = no` — Restart the service. Takes `no`, `always`, `on-failure` and `on-success`. -* `service_workdir = /` -* `service_pidfile` — If the service manages its own pidfile, set this. -* `service_pidfile_timeout = 15` — How long to wait for the service to create its pidfile. -* `service_pidfile_remove_stale = yes` — Remove stale pidfiles before spawning the service process. -* `service_logfile_out = "$logdir/$service_name.log"` — Logfile for output. -* `service_logfile_err = "$service_logfile_out"` — Logfile for stderr. -* `service_stop_timeout = 30` — How long to wait after sending the stop command. -* `service_ready_timeout = 15` — How long to wait till the service is ready, in seconds. -* `service_stop_signal = 15` — Which signal to send to the service when stopping. -* `service_reload_signal = 1` — Which signal to send to the service when reloading. -* `service_cgroup_exclusive = no` — Refuse to start the service if the cgroup is not empty. -* `service_cgroup_wait = no` — Wait on the entire cgroup to die when stopping the service. -* `service_cgroup_strict = 1` — Refuse to do anything if the service's recorded PID is both running and not in the service's appropriate cgroup. This usually means you've either found a bug or something bad happened to your system. -* `service_cgroup_kill = no` — Kill the entire cgroup when stopping the service. -* `service_cgroup_kill_signal = 15` — Which signal to use for that. -* `service_cgroup_cleanup = no` — Kill the entire cgroup on watchdog cleanup. Note that it is distinctly not the same as killing the cgroup on stopping. -* `service_success_exit = 0` — Array. Which exit codes to treat as successful termination. Only works for managed services (With no custom pidfile). -* `service_oneshot = no` — The service is supposed to do something and die instead of daemonizing. -* `service_signals_passthru` — Array, empty by default. Which signals should the svc pass directly to the service mainpid. This is dangerous, use with caution. +* `start` — start the service. Waits for it to become ready. +* `stop` — stop the service. +* `reload` — send a reload signal (default: SIGHUP). +* `restart` — stop then start. +* `status` — print a status line and exit with a meaningful code. +* `qstatus` — quiet status for use in scripts (same logic, no output). +* `info` — print the service's details. +* `logs` — print the service's logfile path(s), or page the combined log. +* `reset-exit` — forget the last exit status ("reset failed"). +* `edit` — edit the service's config file with `$EDITOR`. +* `ready` — an overridable hook the ready-waiter polls. The default is a + no-op. Override it in a service to wait on something real (e.g. a port + listening or a marker file). -Mind your expansions. +Global flags: + +* `-L`, `--list-services` — list known services and their status. +* `-e`, `--edit` — edit the service's config file. +* `-r`, `--reread` — re-read the service's config from its original path + (drops the remembered copy). +* `-f`, `--forget` — forget a remembered service and remove it from `$rundir/current`. +* `-l`, `--load` — load the service (print the loaded-from line) and exit. +* `--reset-exit` — same as the `reset-exit` action. +* `-i`, `--info` — shortcut for the `info` action. +* `--no-netns` — don't re-exec into the service's network namespace + (see `service_netns` below). +* `-h`, `--help` — show usage. + +Everything after `--` is treated as a literal service argument. + +## Service files + +Each service is a plain BASH script: + +```bash +#!/usr/bin/env ssm + +service_respawn = on-failure +service_command = /usr/bin/example -a --long-arg "parameter" +``` + +It is *sourced* by `ssm`, so it is allowed to assign variables and define +functions. Honoured hooks: + +* `pre_` / `post_` — run before/after any action. +* `service::` — highest-precedence override for any action, + e.g. `service::pre_start`. Takes precedence over `` and over the + overloadable defaults. +* `setup` — run at the top of every respawn loop. +* `cleanup` — runs when the svc loop is being torn down (after the + standard stop logic and any cgroup-cleanup). +* `service::setup`/`service::cleanup` work too. + +`service_command` may be a single string or an array. If set, ssm runs it in +the background and tracks it via a pidfile; with `service_pidfile` set, the +service is expected to manage its own pidfile (and ssm waits for it to +appear, then treats any exit as a failure). + +### Options + +Mandatory: + +* `service_command` — the command line to run. + +Optional (defaults shown): + +* `service_respawn = no` — `no`, `always`, `on-failure`, `on-success`. + `always` respawns regardless of how it exits; the others only respawn on + the matching outcome. After 3 failures in 5 seconds ssm gives up + (unless the service is set to force a respawn). +* `service_oneshot = no` — the service does one thing and dies. Use + `--reset-exit` (or the `reset-exit` action) to let it run again. +* `service_workdir = /` — working directory to `cd` into before running. +* `service_pidfile` — if the service manages its own pidfile, set this. + ssm will wait for the pidfile to appear (`service_pidfile_timeout`), then + treat any termination as a failure. +* `service_pidfile_timeout = 15` — seconds to wait for the pidfile. +* `service_pidfile_remove_stale = 1` — remove stale pidfiles from services + with their own pidfile before spawning. +* `service_logfile_out = $logdir/.log` — stdout log. +* `service_logfile_err = $service_logfile_out` — stderr log; set it to the + same as the out log to merge. +* `service_nologs` — discard all output. +* `service_stop_timeout = 30` — seconds to wait after the stop signal. +* `service_ready_timeout = 15` — seconds to wait for the ready check. +* `service_stop_signal = 15` — signal sent to stop the service. +* `service_stop_command` — full command used to stop; defaults to + `kill -n "$service_stop_signal" "$job_pid"`. +* `service_reload_signal = 1` — signal sent by `reload`. +* `service_success_exit = 0` — array of exit codes treated as success + (managed services only; with no custom pidfile). +* `service_depends` — array of services that must be started (or at least + started-and-ready) first. +* `service_depends_ready` — array of services that must be *ready* first. +* `service_tmpfiles` — array of `"path:type:args"` to create before start: + * `"path:symlink:target"` + * `"path:file:perms:owner:group"` + * `"path:dir:perms:owner:group"` (defaults: `644`/`755`, `root:root`; + all optional) +* `service_remember = 1` — copy the config into `$rundir/current` on first + use. Disable to keep the config read-only in place. +* `service_netns` — if set, ssm re-execs itself inside this network + namespace (`ip netns exec`). Use `--no-netns` to bypass. +* `service_cgroup_*` options — see "cgroups" below. Only meaningful with + `cgroups = 1` in the global config. +* `service_args` / `service_type` — legacy spellings; `service_type = oneshot` + is equivalent to `service_oneshot = yes` and `service_respawn = 1` is + equivalent to `always`. + +## Global config + +`/etc/ssm/ssm.conf` (system) and `$XDG_CONFIG_HOME/ssm/ssm.conf` (user). +Since it is sourced, it is plain BASH: + +```bash +# Enable cgroup-related features (experimental, cgroups v2) +cgroups = 1 + +# Override the service search path (PATH-like array; these are appended, +# not replaced, after the built-in order) +service_path = "$XDG_CONFIG_HOME/ssm/services" /etc/ssm/services /usr/share/ssm/services + +# Service defaults +service_respawn = no +service_workdir = / +service_stop_timeout = 30 +service_ready_timeout = 15 +service_reload_signal = 1 +service_stop_signal = 15 + +# Cgroup-related defaults (see below) +service_cgroup_exclusive = 0 +service_cgroup_wait = 0 +service_cgroup_strict = 1 +service_cgroup_kill = 0 +service_cgroup_kill_signal = 15 +service_cgroup_cleanup = 0 +``` + +## cgroups + +Experimental, cgroups v2, and frankly a footgun — leave it off unless you +really know what you're doing. When enabled: + +* the service's processes live in `$cgroup_home/$service_name` + (`/sys/fs/cgroup/ssm/…` for system services, + `/sys/fs/cgroup/user/$UID/ssm/…` for user services); +* `service_cgroup_exclusive` — refuse to start if the cgroup is non-empty; +* `service_cgroup_wait` — wait on the whole cgroup to die on stop; +* `service_cgroup_strict` — refuse to act if the recorded service PID is + not in the service's cgroup (default 1); +* `service_cgroup_kill` / `service_cgroup_kill_signal` — kill the entire + cgroup on stop; +* `service_cgroup_cleanup` — kill stray members of the cgroup when the + main PID exits. + +## Exit codes + +* `0` — success +* `1` — generic failure +* `2` — usage error +* `3` — service already running (start) / not running (stop) +* `5` — ready/stop timeout +* `7` — dependency failure / oneshot service cannot be stopped +* `9` — command does not exist +* `13` — failed to create tmpfiles +* `15` — cgroup not empty (`service_cgroup_exclusive`) +* `17` — service failed (oneshot) / action not defined +* `19` — service already enabled (start) / service not found +* `75` — svc is not running but its recorded main PID is (stale state) + +`status` exits 0 (running), 1 (not running), 7 (stopped), 9 (failed) or +11 (exited), and `qstatus` is the same logic without output. + +## See also + +The [ssm-services](https://code.fleshless.org/Spark/ssm-services) repository +has example service files. + +## License + +See `LICENSE`.