From f74df5962e1c132f85cc32a8804c8d415676560e Mon Sep 17 00:00:00 2001 From: fbt Date: Fri, 27 Nov 2026 16:14:30 +0000 Subject: [PATCH] Better examples Signed-off-by: fbt --- examples/services/backup | 14 ++++++++++++++ examples/services/cron | 9 +++++++++ examples/services/iptables | 29 ++++++++++++++++++++++------- examples/services/logrotate | 11 +++++++++++ examples/services/nginx | 24 +++++++++++++++++++----- examples/services/postgres | 18 ++++++++++++++++++ examples/services/rc.local | 16 ++++++++++------ examples/services/sshd | 23 ++++++++++++++++++----- examples/services/tinc | 15 +++++++++++++-- examples/services/webapp | 16 ++++++++++++++++ ssm | 2 ++ 11 files changed, 152 insertions(+), 25 deletions(-) create mode 100644 examples/services/backup create mode 100644 examples/services/cron create mode 100644 examples/services/logrotate create mode 100644 examples/services/postgres create mode 100644 examples/services/webapp diff --git a/examples/services/backup b/examples/services/backup new file mode 100644 index 0000000..8c16162 --- /dev/null +++ b/examples/services/backup @@ -0,0 +1,14 @@ +#!/usr/bin/env ssm + +# Backup rotation — oneshot service with tmpfiles. +# +# service_tmpfiles creates paths before start: +# "path:file:perms:owner:group" +# "path:dir:perms:owner:group" +# "path:symlink:target" +# Directories default to 755, files to 644, owners default to root:root. + +service_oneshot = yes +service_tmpfiles = /var/backup:dir:0755:backup:backup \ + /var/backup/manifest:file:0640:root:backup +service_command = /usr/local/bin/backup-rotate --config /etc/backup.conf diff --git a/examples/services/cron b/examples/services/cron new file mode 100644 index 0000000..685885d --- /dev/null +++ b/examples/services/cron @@ -0,0 +1,9 @@ +#!/usr/bin/env ssm + +# cron daemon. +# +# cron forks and daemonizes by default; -f keeps it in the foreground so +# ssm can track it directly. + +service_respawn = always +service_command = /usr/sbin/cron -f diff --git a/examples/services/iptables b/examples/services/iptables index 745743f..57f4dd9 100755 --- a/examples/services/iptables +++ b/examples/services/iptables @@ -1,11 +1,26 @@ #!/usr/bin/env ssm -service_oneshot = true -service_command = /usr/bin/false +# nftables ruleset. +# +# There is no nftables daemon: the ruleset is applied at startup and +# stays in the kernel until changed. We therefore treat this as a oneshot +# service whose real job is to apply the ruleset. -service::pre_start() { - cat <<- EOF - Please don't. Use ferm or any other wrapper. - You will just reinvent one in here anyway. - EOF +service_oneshot = yes +service_command = /usr/sbin/nft -f /etc/nftables.conf + +var nftables_conf = /etc/nftables.conf + +check_config() { + [[ ! -f "$nftables_conf" ]] && err "nftables config not found: $nftables_conf" +} + +pre_start() { + check_config || return 17 +} + +reload() { + check_config || return 17 + + /usr/sbin/nft -f "$nftables_conf" } diff --git a/examples/services/logrotate b/examples/services/logrotate new file mode 100644 index 0000000..c2c8854 --- /dev/null +++ b/examples/services/logrotate @@ -0,0 +1,11 @@ +#!/usr/bin/env ssm + +# logrotate — a oneshot service. +# +# This service runs once and exits. ssm marks it "enabled" after a +# successful run; use the reset-exit action (or `ssm logrotate reset-exit`) +# to let it run again. Schedule the service to run from whatever timer +# you prefer (systemd timer, cron, ...). + +service_oneshot = yes +service_command = /usr/sbin/logrotate -f /etc/logrotate.conf diff --git a/examples/services/nginx b/examples/services/nginx index 8389843..0d5a0af 100755 --- a/examples/services/nginx +++ b/examples/services/nginx @@ -1,8 +1,22 @@ #!/usr/bin/env ssm -service_respawn = on-failure -service_command = /usr/bin/nginx -service_pidfile = /run/nginx.pid +# nginx web server. +# +# nginx daemonizes itself (the parent returns after handing off to +# workers), so we don't wait for the process to exit — we wait for the +# port instead. -# Do not reload the service if the config test fails. -pre_reload() { "$service_command" -t "$@"; } +service_respawn = on-failure +service_command = /usr/sbin/nginx +service_pidfile = /run/nginx.pid +service_pidfile_timeout = 10 + +# ngs sometimes takes a moment to bind; poll until the socket answers. +ready() { + (exec 3<>/dev/tcp/127.0.0.1/80) 2>/dev/null +} + +# Do not send the reload signal if the config doesn't parse. +pre_reload() { + "$service_command" -t +} diff --git a/examples/services/postgres b/examples/services/postgres new file mode 100644 index 0000000..3dc9ef0 --- /dev/null +++ b/examples/services/postgres @@ -0,0 +1,18 @@ +#!/usr/bin/env ssm + +# PostgreSQL. +# +# postgres writes its own postmaster.pid file, so ssm doesn't create one +# — instead it waits for the pidfile to appear and treats any process +# exit as a failure. This is how services with their own pidfile should +# be written. + +service_respawn = on-failure +service_command = /usr/lib/postgresql/17/bin/postgres -D /var/lib/postgresql/data +service_pidfile = /var/lib/postgresql/data/postmaster.pid +service_pidfile_timeout = 30 + +# Wait for the server to actually accept connections. +ready() { + (exec 3<>/dev/tcp/127.0.0.1/5432) 2>/dev/null +} diff --git a/examples/services/rc.local b/examples/services/rc.local index f22822d..73fe9b1 100755 --- a/examples/services/rc.local +++ b/examples/services/rc.local @@ -1,9 +1,13 @@ #!/usr/bin/env ssm -service_type = oneshot -service_command = /etc/rc.local +# /etc/rc.local — a oneshot service that runs whatever the system admin +# drops there. Missing or non-executable files are not an error; the +# system may legitimately have no rc.local. -pre_start() { - # Do nothing, successfully, if /etc/rc.local is not executable. - [[ -x "/etc/rc.local" ]] || die 0 -} +service_oneshot = yes + +if [[ -x /etc/rc.local ]]; then + service_command = /etc/rc.local +else + service_command = /bin/true +fi diff --git a/examples/services/sshd b/examples/services/sshd index ddea5d3..469d4fc 100755 --- a/examples/services/sshd +++ b/examples/services/sshd @@ -1,10 +1,23 @@ #!/usr/bin/env ssm -service_respawn = always -service_command = /usr/bin/sshd -D -f "/etc/ssh/sshd_config" +# OpenSSH server. +# +# sshd is run in foreground (-D) so ssm can track the process directly. +# Host keys are created if they're missing; sshd refuses to start +# without them, so do it before service_command is spawned. + +service_respawn = on-failure +service_command = /usr/sbin/sshd -D -f /etc/ssh/sshd_config +service_ready_timeout = 10 pre_start() { - if ! [[ -e "/etc/ssh/ssh_host_key" ]]; then - ssh-keygen -A - fi + if [[ ! -f /etc/ssh/ssh_host_rsa_key ]]; then + ssh-keygen -A + fi +} + +# Wait for sshd to answer on port 22, not just for the parent process +# to return. +ready() { + (exec 3<>/dev/tcp/127.0.0.1/22) 2>/dev/null } diff --git a/examples/services/tinc b/examples/services/tinc index a3c8366..e68b951 100755 --- a/examples/services/tinc +++ b/examples/services/tinc @@ -1,7 +1,18 @@ #!/usr/bin/env ssm -# This trick was neat back in 2011, I swear -var tinc_network = "${service_name##*-}" +# One tinc network per service instance. +# +# The service name is expected to look like "tinc-", e.g. +# "tinc-vpn" for the network configured in /etc/tinc/vpn. + +var tinc_network = "${service_name#tinc-}" service_respawn = on-failure service_command = /usr/bin/tincd -D -n "$tinc_network" + +pre_start() { + if [[ ! -d "/etc/tinc/$tinc_network" ]]; then + err "tinc network '$tinc_network' has no config directory" + return 17 + fi +} diff --git a/examples/services/webapp b/examples/services/webapp new file mode 100644 index 0000000..8ccf45c --- /dev/null +++ b/examples/services/webapp @@ -0,0 +1,16 @@ +#!/usr/bin/env ssm + +# Example application that depends on PostgreSQL. +# +# service_depends_ready makes ssm start the postgres service and wait +# for it to report ready before starting this one. + +service_respawn = always +service_depends_ready = postgres +service_command = /usr/bin/webapp --config /etc/webapp/config.toml + +# Wait for the app to answer on its HTTP port. ssm polls this hook until +# the ready timeout expires. +ready() { + (exec 3<>/dev/tcp/127.0.0.1/8080) 2>/dev/null +} diff --git a/ssm b/ssm index d5ef5bd..70c1406 100755 --- a/ssm +++ b/ssm @@ -123,6 +123,8 @@ die() { exit "$code" }; readonly -f die +err() { printf '%s\n' "$@" >&2; } + if_service_action() { for f in "service::$1" "$1"; do is_function "$f" && return 0