feat: add systemd service config and install script

config/systemd/user/claude-sessions-save.service: source of truth
for the shutdown service, version-controlled alongside the scripts
it calls.

scripts/install-systemd.sh: idempotent install script — copies the
service file to ~/.config/systemd/user/, reloads the daemon, and
enables + starts the service. Run on any new machine to wire up
graceful Claude session shutdown.
This commit is contained in:
Paul O'Reilly
2026-10-12 16:11:18 +13:00
parent 2335a73a02
commit 53170c320e
2 changed files with 69 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
[Unit]
Description=Gracefully exit Claude Code sessions before shutdown
Documentation=https://github.com/oreillyit/claude-foundations
# Run before the system begins tearing down the session
Before=shutdown.target halt.target reboot.target poweroff.target
[Service]
Type=oneshot
RemainAfterExit=yes
# ExecStart is a no-op — the service exists purely for its ExecStop
ExecStart=/bin/true
# On stop (triggered at shutdown): gracefully exit all Claude tmux sessions
ExecStop=%h/dev/claude/projects/claude-foundations/scripts/claude-sessions-shutdown
# Allow up to 120s for Claude sessions to save and exit cleanly
TimeoutStopSec=120
[Install]
WantedBy=default.target

51
scripts/install-systemd.sh Executable file
View File

@@ -0,0 +1,51 @@
#!/usr/bin/env bash
# install-systemd.sh — Install claude-sessions systemd user service.
#
# Copies the service file from this repo into ~/.config/systemd/user/,
# reloads the daemon, and enables + starts the service.
#
# Safe to re-run (idempotent).
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_DIR="$(dirname "$SCRIPT_DIR")"
SRC="$REPO_DIR/config/systemd/user/claude-sessions-save.service"
DST_DIR="$HOME/.config/systemd/user"
DST="$DST_DIR/claude-sessions-save.service"
GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
ok() { echo -e "${GREEN}OK${NC} $*"; }
skip() { echo -e "${YELLOW}SKIP${NC} $*"; }
[[ -f "$SRC" ]] || { echo "ERROR: source not found: $SRC" >&2; exit 1; }
mkdir -p "$DST_DIR"
if [[ -f "$DST" ]] && diff -q "$SRC" "$DST" &>/dev/null; then
skip "service file already up to date"
else
cp "$SRC" "$DST"
ok "installed $DST"
fi
systemctl --user daemon-reload
ok "daemon reloaded"
if systemctl --user is-enabled --quiet claude-sessions-save.service 2>/dev/null; then
skip "already enabled"
else
systemctl --user enable claude-sessions-save.service
ok "enabled claude-sessions-save.service"
fi
if systemctl --user is-active --quiet claude-sessions-save.service 2>/dev/null; then
skip "already active"
else
systemctl --user start claude-sessions-save.service
ok "started claude-sessions-save.service"
fi
echo ""
echo "claude-sessions-save.service is enabled and active."
echo "It will gracefully exit Claude tmux sessions at shutdown."