From 5456aa2728a093db8c7565be67eafd3cf05455dc Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Thu, 12 Apr 2012 22:12:58 -0500 Subject: [PATCH] add tmux-smart-multiattach.sh --- tmux-smart-multiattach.sh | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 tmux-smart-multiattach.sh diff --git a/tmux-smart-multiattach.sh b/tmux-smart-multiattach.sh new file mode 100755 index 0000000..85993bc --- /dev/null +++ b/tmux-smart-multiattach.sh @@ -0,0 +1,31 @@ +#!/bin/sh + +host=`hostname` + +tmux has-session -t $host +if [ $? -eq 1 ]; then + # no session, creating in daemon mode + tmux -u new-session -s $host -d + echo "created new session: $host" +fi + +# session exists or we just created it, now figure out whether +# to attach to session $host or create a new clone of it + +attached=`tmux list-sessions | grep attached | grep ^$host:` +if [ ".$attached" = "." ]; then + # nothing attached to session $host, so attach to it + echo "attaching to session: $host" + exec tmux -u attach-session -t $host +else + # session $host is attached, so create a new one + # we need a new name for this clone. cheap: name-pid + sessionname=$host-$$ + + echo "creating new session grouped to $host: $sessionname" + tmux -u new-session -t $host -s $sessionname + + # getting here means client detached. delete this session + echo "deleting clone session: $sessionname" + tmux -u kill-session -t $sessionname +fi