add tmux-smart-multiattach.sh

This commit is contained in:
Brian S. Stephan 2012-04-12 22:12:58 -05:00
parent 835b10cf91
commit 5456aa2728
1 changed files with 31 additions and 0 deletions

31
tmux-smart-multiattach.sh Executable file
View File

@ -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