#!/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