Last active
April 11, 2026 09:10
-
-
Save mhrstmnn/70db17f02275a78260f36c158182cfbc to your computer and use it in GitHub Desktop.
Utility functions to manage tmux sessions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/zsh | |
| tmux_session_exists() { | |
| local session_name="$1" | |
| [[ -z "$session_name" ]] && return 1 | |
| tmux has-session -t "$session_name" 2> /dev/null | |
| } | |
| tmux_start() { | |
| local session_name="$1" | |
| local command="$2" | |
| if [[ -z "$session_name" || -z "$command" ]]; then | |
| echo "Usage: tmux_start <session_name> <command>" | |
| return 1 | |
| fi | |
| if tmux_session_exists "$session_name"; then | |
| echo "Session '$session_name' is already running." | |
| else | |
| echo "Starting session '$session_name' ..." | |
| tmux new-session -d -s "$session_name" "$command" | |
| fi | |
| } | |
| tmux_attach() { | |
| local session_name="$1" | |
| if [[ -z "$session_name" ]]; then | |
| echo "Usage: tmux_attach <session_name>" | |
| return 1 | |
| fi | |
| if tmux_session_exists "$session_name"; then | |
| echo "Attaching to session '$session_name' ..." | |
| tmux attach-session -t "$session_name" | |
| else | |
| echo "Session '$session_name' does not exist." | |
| return 1 | |
| fi | |
| } | |
| tmux_stop() { | |
| local session_name="$1" | |
| if [[ -z "$session_name" ]]; then | |
| echo "Usage: tmux_stop <session_name>" | |
| return 1 | |
| fi | |
| if tmux_session_exists "$session_name"; then | |
| echo "Stopping '$session_name' ..." | |
| tmux send-keys -t "$session_name" C-c | |
| sleep 3 # Give it time to clean up | |
| if tmux_session_exists "$session_name"; then | |
| tmux kill-session -t "$session_name" | |
| echo "Session '$session_name' forced to terminate." | |
| else | |
| echo "Session '$session_name' exited gracefully." | |
| fi | |
| else | |
| echo "Session '$session_name' does not exist." | |
| return 1 | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment