#!/bin/sh set -eu [ -n "${DEBUG:-}" ] && set -x SYS_RT=/run USER_RT=${XDG_RUNTIME_DIR:-/nouser} # fallback when not set main() { # NOTE: this assumes that: # system services have their live dir at /run/s6-rc # user services have their live dir at $XDG_RUNTIME_DIR/s6-rc local rt=$SYS_RT [ "${1:-}" = "-u" ] && rt=$USER_RT && shift [ "${UID:-$(id -u)}" -ne 0 ] && rt=$USER_RT local l=$rt/s6-rc # NOTE: this assumes that: # $l/compiled is a symlink to a directory inside the confdir # which contains a directory src, defining the s6-rc source local confdir confdir=$(readlink -e "$l/compiled") confdir=$(dirname "$confdir") test -d "$confdir/src" local cmd=${1:-} shift case "$cmd" in ## ## db ## reload | recompile) export LIVEDIR="$l" export STATEDIR="$confdir" export SRCDIR="$confdir/src" exec s6-rc-recompile ;; ## ## service ## up | start) exec s6-rc -v2 -l "$l" -b start $@ ;; dn | down | stop) exec s6-rc -v2 -l "$l" -b stop $@ ;; re | restart) $0 dn $@ && $0 up $@ ;; st | status | info) local sv=${1:?} s6-rc-db -l "$l" type $sv >/dev/null 2>&1 || exit 1 printf 'Service (%s):\n' $(s6-rc-db -l "$l" type $sv) for ln in $(s6-rc-db -l "$l" atomics $sv); do printf '%s: ' $ln s6-svstat "$l/servicedirs/$ln" done | sort -k 2 | column -s ':' -t printf '\nDependencies:\n' s6-rc-db -l "$l" dependencies $sv # NOTE: this assumes logs are stored in $rt/log logfile="$rt/log/$sv/current" if test -f "$logfile"; then printf '\nLogs (%s):\n' "$logfile" tail -n 10 "$logfile" fi ;; logtail | logs) local sv=${1:?} # NOTE: this assumes logs are stored in $rt/log logfile="$rt/log/$sv/current" if test -f "$logfile"; then tail -f "$logfile" fi ;; ## ## bundles ## bundle-ls) find "$confdir/src" -mindepth 1 -maxdepth 1 -exec sh -c 'test "$(cat {}/type)" = "bundle" && basename {}' \; ;; bundle-add) # TODO: add confirmation? local sv=${1:?} local bundle=${2:?} touch "$confdir/src/$bundle/contents.d/$sv" ;; bundle-del) # TODO: add confirmation? local sv=${1:?} local bundle=${2:?} rm "$confdir/src/$bundle/contents.d/$sv" ;; ## ## listall ## ls | list | ps | *) [ "${1:-}" = "-a" ] && exec s6-rc-db -l "$l" list services exec s6-rc -v2 -l "$l" -a list ;; esac } main $@