#!/usr/bin/env bash # # find-sessions.sh - Discover and list tmux sessions on sockets # # PURPOSE: # Find and display information about tmux sessions, either on a specific # socket or by scanning all sockets in a directory. Useful for discovering # what agent sessions are currently running. # # HOW IT WORKS: # 1. Identify target socket(s) based on command-line options # 2. Query each socket for running tmux sessions # 3. Display session info: name, attach status, creation time # 4. Optionally filter by session name substring # # USE CASES: # - List all agent sessions across multiple sockets # - Find a specific session by name (partial matching) # - Check if a session is attached or detached # - See when sessions were created # - Enumerate sessions before cleanup # # EXAMPLES: # # List sessions on default tmux socket # ./find-sessions.sh # # # List sessions on specific socket by name # ./find-sessions.sh -L mysocket # # # List sessions on specific socket by path # ./find-sessions.sh -S /tmp/claude-tmux-sockets/claude.sock # # # Scan all sockets in directory # ./find-sessions.sh --all # # # Find sessions with "python" in the name # ./find-sessions.sh --all -q python # # DEPENDENCIES: # - bash (with arrays, [[, functions) # - tmux (for list-sessions) # - grep (for filtering by query) # # Bash strict mode: # -e: Exit immediately if any command fails # -u: Treat unset variables as errors # -o pipefail: Pipe fails if any command in pipeline fails set -euo pipefail usage() { cat <<'USAGE' Usage: find-sessions.sh [-L socket-name|-S socket-path|-A] [-q pattern] List tmux sessions on a socket (default tmux socket if none provided). Options: -L, --socket tmux socket name (passed to tmux -L) -S, --socket-path tmux socket path (passed to tmux -S) -A, --all scan all sockets under CLAUDE_TMUX_SOCKET_DIR -q, --query case-insensitive substring to filter session names -h, --help show this help USAGE } # ============================================================================ # Default Configuration # ============================================================================ # Socket specification (mutually exclusive) socket_name="" # tmux socket name (for tmux -L) socket_path="" # tmux socket path (for tmux -S) # Filtering and scanning options query="" # substring to filter session names (case-insensitive) scan_all=false # whether to scan all sockets in socket_dir # Directory containing agent tmux sockets # Priority: CLAUDE_TMUX_SOCKET_DIR env var > TMPDIR/claude-tmux-sockets > /tmp/claude-tmux-sockets socket_dir="${CLAUDE_TMUX_SOCKET_DIR:-${TMPDIR:-/tmp}/claude-tmux-sockets}" # ============================================================================ # Parse Command-Line Arguments # ============================================================================ while [[ $# -gt 0 ]]; do case "$1" in -L|--socket) socket_name="${2-}"; shift 2 ;; # Socket name mode -S|--socket-path) socket_path="${2-}"; shift 2 ;; # Socket path mode -A|--all) scan_all=true; shift ;; # Scan all mode -q|--query) query="${2-}"; shift 2 ;; # Filter by name -h|--help) usage; exit 0 ;; # Show help *) echo "Unknown option: $1" >&2; usage; exit 1 ;; # Error on unknown esac done # ============================================================================ # Validate Options # ============================================================================ # Cannot use --all with specific socket options (they're mutually exclusive) if [[ "$scan_all" == true && ( -n "$socket_name" || -n "$socket_path" ) ]]; then echo "Cannot combine --all with -L or -S" >&2 exit 1 fi # Cannot use both -L and -S at the same time (different socket types) if [[ -n "$socket_name" && -n "$socket_path" ]]; then echo "Use either -L or -S, not both" >&2 exit 1 fi # Check that tmux is installed and available in PATH if ! command -v tmux >/dev/null 2>&1; then echo "tmux not found in PATH" >&2 exit 1 fi # ============================================================================ # Function: list_sessions # ============================================================================ # Query a tmux socket for sessions and display formatted output # # Arguments: # $1: Label describing the socket (for display purposes) # $@: Remaining args are passed to tmux command (e.g., -L name or -S path) # # Returns: # 0 if sessions found (or no sessions after filtering) # 1 if tmux server not running on this socket # # Output format: # Sessions on