Files
ssm/README.md
T

263 lines
10 KiB
Markdown
Raw Normal View History

# ssm
2017-07-13 15:45:56 +03:00
2026-11-29 10:27:48 +00:00
Yet another service manager. Think of this one as init scripts on steroids.
2017-07-13 15:45:56 +03:00
Services are BASH scripts with a tiny DSL that get *sourced* by `ssm`. Same
pitfalls apply — keep complex logic out of them.
2018-03-05 04:28:35 +03:00
2026-11-29 12:09:03 +00:00
## Pitfalls
The script will misbehave in annoying and inconvenient ways unless it runs
2026-11-29 12:13:03 +00:00
under a proper init that can reap children. Architecture limitation, wontfix.
2026-11-29 12:09:03 +00:00
## Install
2018-03-09 02:31:14 +03:00
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):
2018-03-05 04:28:35 +03:00
* `$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`
2018-03-05 04:28:35 +03:00
`$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.
2018-03-05 04:28:35 +03:00
## Usage
2018-03-05 04:28:35 +03:00
```
ssm <service> <action>
```
2018-03-05 04:28:35 +03:00
Actions (or service-provided functions):
2018-03-05 04:28:35 +03:00
* `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).
2018-03-08 23:57:44 +03:00
2026-11-27 15:12:10 +00:00
Arguments after the action are passed into the service script when it's sourced and
thus can be used as positional arguments on the inside:
```
#!/usr/bin/env ssm
service_command = /path/to/executable --flag "$1" --flag1 "$2"
```
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.
## Service files
Each service is a BASH script written in a tiny DSL (see `var()` below), pure BASH is supported:
```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_<action>` / `post_<action>` — run before/after any action.
* `service::<action>` — highest-precedence override for any action,
e.g. `service::pre_start`. Takes precedence over `<action>` 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:
2026-11-29 15:07:40 +00:00
* `service_command` — the command line to run. Has to be a full path!
2026-11-29 15:09:32 +00:00
If PATH interpolation is still needed, do
`service_command = $(which command)`
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/<name>.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`.
## `var()`
Everything ssm sources — the global config, service files and `functions/`
snippets — runs inside ssm's shell, where the built-in `var()` helper is
available. `var` turns an array-valued bash variable into a small ad-hoc
interface: `var <name>` creates a `<name>` accessor function (and
`<name>++` / `<name>--`) that acts on an array variable of the same name.
Declared functions are read-only. Using the accessor bare tests truthiness;
other operators are evaluated in place and leave their result in `$?`, e.g.
under `if`/`&&`/`||`.
```bash
var timeout = 5 # set (values become an array)
var service_args = -a --long-arg "parameter"
service_args u --long-arg # membership in the array
timeout == 5 # compare first element against a value
timeout =~ '^[0-9]+$' # regex match
```
Operators:
* `var name = value [value...]` — set the variable (it becomes a bash array).
* `var name += value [value...]` — append values.
* `var name _= value [value...]` — prepend values.
* `var name := value [value...]` — set only if the variable is unset or empty.
* `var name == value` — exit 0 if the variable's first element equals `value`.
* `var name =~ regex` — exit 0 if the variable's first element matches `regex`.
* `var name is file` / `is dir` / `is directory` / `is fs_object` / `is empty` —
filesystem-style tests on the value.
* `var name u value` — exit 0 if `value` is one of the array's elements.
* `var name` — no operator: exit 0 unless the variable is unset, empty, or
`false`/`0` (compared case-insensitively).
* `name++` / `name--` — increment/decrement the first element.
* `var name -v other_name` — build the accessor for `other_name` instead of
`name` (so `name` acts on `other_name`).
## Global config
`/etc/ssm/ssm.conf` (system) and `$XDG_CONFIG_HOME/ssm/ssm.conf` (user).
Since it is sourced, it is a BASH script with a DSL (see `var()` above):
```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
2026-11-28 11:10:46 +00:00
Cobwebs and dust cover this section. This feature is as of today not in
2026-11-27 14:33:29 +00:00
line with how cgroups are supposed to be used. Not sure I care to fix that.
* 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`.