From cae4fb87664636f89b4562cef3f3f32fe4f56a1d Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Thu, 8 Oct 2026 10:17:02 -0400 Subject: [PATCH] feat(cli): add kitty-day-save script to flush sessions on shutdown --- dot_local/bin/executable_kitty-day-save | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 dot_local/bin/executable_kitty-day-save diff --git a/dot_local/bin/executable_kitty-day-save b/dot_local/bin/executable_kitty-day-save new file mode 100755 index 0000000..986dae7 --- /dev/null +++ b/dot_local/bin/executable_kitty-day-save @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +# Save every kitty daily session (~/.config/kitty/day_session.py) right before +# shutdown/reboot. +# +# kitty-day-save.service runs this under a logind "delay" inhibitor. When a +# shutdown or reboot starts, logind broadcasts PrepareForShutdown(true) and +# waits (InhibitDelayMaxSec, 5 s by default) for the lock to be released, which +# happens when this script exits. Nothing has been killed yet at that point, so +# every kitty still has all of its windows. The flush also stops further saves +# for a minute, so windows dying during the shutdown can't shrink the saved day. +# +# Usage: kitty-day-save hold the lock, wait for a shutdown, flush, exit +# kitty-day-save --now flush every running kitty immediately +set -u + +case "${1-}" in +--now) + # Every kitty on kitty.conf listens on @kitty-; ones that don't own + # a day session ignore the flush. + for s in $(grep -oE '@kitty-[0-9]+$' /proc/net/unix | sort -u); do + timeout 3 kitten @ --to "unix:$s" action kitten day_session.py flush >/dev/null 2>&1 & + done + wait + ;; +--wait) + # grep returns on the first match; gdbus is left to the service's cgroup. + grep -m1 -q 'PrepareForShutdown (true' < <( + gdbus monitor --system --dest org.freedesktop.login1 --object-path /org/freedesktop/login1 + ) && exec "$0" --now + ;; +*) + exec systemd-inhibit --what=shutdown --mode=delay --who=kitty \ + --why='Saving kitty sessions' "$0" --wait + ;; +esac