about summary refs log tree commit diff
path: root/bin/sv
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2025-09-22 18:36:58 +0200
committerRobert Günzler <r@gnzler.io>2025-09-22 19:53:54 +0200
commit78e1a5e6df2cbd4e4dafb15d18345ab94825e317 (patch)
treef2213bfd48209db2741f04de211cef48b73bae97 /bin/sv
initial commit
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to 'bin/sv')
-rwxr-xr-xbin/sv117
1 files changed, 117 insertions, 0 deletions
diff --git a/bin/sv b/bin/sv
new file mode 100755
index 0000000..2f2bcf2
--- /dev/null
+++ b/bin/sv
@@ -0,0 +1,117 @@
+#!/bin/sh
+set -eu
+[ -n "${DEBUG:-}" ] && set -x
+
+SYS_RT=/run
+USER_RT=$XDG_RUNTIME_DIR
+
+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 $@