zellij cleanup + small files
This commit is contained in:
Executable
+102
@@ -0,0 +1,102 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# Cleans up zellij sessions that are inactive:
|
||||
# - sessions marked EXITED (resurrectable metadata)
|
||||
# - running sessions with 0 attached clients
|
||||
#
|
||||
# Usage:
|
||||
# cleanup-zellij-inactive.sh # delete inactive sessions
|
||||
# cleanup-zellij-inactive.sh --dry-run # show what would be deleted
|
||||
|
||||
DRY_RUN=0
|
||||
|
||||
case "${1-}" in
|
||||
"" ) ;;
|
||||
-n|--dry-run) DRY_RUN=1 ;;
|
||||
-h|--help)
|
||||
cat <<'EOF'
|
||||
cleanup-zellij-inactive.sh
|
||||
|
||||
Delete zellij sessions that are inactive:
|
||||
- EXITED sessions are deleted
|
||||
- running sessions with 0 attached clients are killed+deleted
|
||||
|
||||
Options:
|
||||
-n, --dry-run Show what would be deleted
|
||||
-h, --help Show this help
|
||||
EOF
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo "Unknown option: $1" >&2
|
||||
echo "Use --help for usage" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
if ! command -v zellij >/dev/null 2>&1; then
|
||||
echo "zellij not found in PATH" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mapfile -t session_lines < <(zellij list-sessions --no-formatting 2>/dev/null || true)
|
||||
|
||||
if [ "${#session_lines[@]}" -eq 0 ]; then
|
||||
echo "No zellij sessions found"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
deleted=0
|
||||
failed=0
|
||||
kept=0
|
||||
|
||||
for line in "${session_lines[@]}"; do
|
||||
[ -z "$line" ] && continue
|
||||
|
||||
name="${line%% *}"
|
||||
is_exited=0
|
||||
if [[ "$line" == *"EXITED"* ]]; then
|
||||
is_exited=1
|
||||
fi
|
||||
|
||||
should_delete=0
|
||||
|
||||
if [ "$is_exited" -eq 1 ]; then
|
||||
should_delete=1
|
||||
else
|
||||
# Running session: check attached clients
|
||||
clients_out="$(zellij --session "$name" action list-clients 2>/dev/null || true)"
|
||||
client_count="$(printf '%s\n' "$clients_out" | tail -n +2 | sed '/^\s*$/d' | wc -l | tr -d ' ')"
|
||||
if [ "$client_count" -eq 0 ]; then
|
||||
should_delete=1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$should_delete" -eq 1 ]; then
|
||||
if [ "$DRY_RUN" -eq 1 ]; then
|
||||
echo "[dry-run] delete: $name"
|
||||
deleted=$((deleted + 1))
|
||||
else
|
||||
# --force also kills running sessions before deleting
|
||||
if zellij delete-session --force "$name" >/dev/null 2>&1; then
|
||||
echo "deleted: $name"
|
||||
deleted=$((deleted + 1))
|
||||
else
|
||||
echo "failed: $name" >&2
|
||||
failed=$((failed + 1))
|
||||
fi
|
||||
fi
|
||||
else
|
||||
kept=$((kept + 1))
|
||||
fi
|
||||
done
|
||||
|
||||
echo
|
||||
if [ "$DRY_RUN" -eq 1 ]; then
|
||||
echo "Would delete: $deleted"
|
||||
else
|
||||
echo "Deleted: $deleted"
|
||||
echo "Failed: $failed"
|
||||
fi
|
||||
echo "Kept: $kept"
|
||||
Reference in New Issue
Block a user