Skip to content

Instantly share code, notes, and snippets.

@dartov
Created July 25, 2012 15:14
Show Gist options
  • Save dartov/3176701 to your computer and use it in GitHub Desktop.
Save dartov/3176701 to your computer and use it in GitHub Desktop.
Scripted tmux to open syncronized and separate windows for a cluster of servers
#!/bin/sh
# A rather simple example of tmux scripting
# Sets a new session for a cluster of listed servers
# Session has a single window with every server in a cluster as separate pane with synchronized input,
# and an additional window for every machine in a cluster
export DISABLE_AUTO_TITLE=true # needed to fix names of windows
SERVER_LIST="server-1 server-2 server-3"
CLUSTER_NAME="some-cluster" # will be a name of the tmux session
CONNECT_STRING='ssh -i ~/.ssh/id_dsa_cluster $SERVER' # password-less login is recommended
window=1 # assuming base-index is set to 1
pane=1 # assuming pane-base-index is set to 1
number_of_servers=`echo $SERVER_LIST | wc -w`
tmux has-session -t $CLUSTER_NAME
if [ $? != 0 ]
then
tmux new-session -s $CLUSTER_NAME -n cluster -d
for fn in $SERVER_LIST; do
SERVER=$fn
eval `eval "echo tmux send-keys -t $CLUSTER_NAME:$window.$pane \'$CONNECT_STRING\' C-m"`
if [ $number_of_servers -gt $pane ]; # little trick to not split for last server
then
let pane++
tmux split-window -h -t $CLUSTER_NAME
fi
done
tmux select-layout -t $CLUSTER_NAME:$window even-horizontal # you might prefer 'tiled' for large cluster
tmux set-window-option -t $CLUSTER_NAME:$window synchronize-panes on
for fn in $SERVER_LIST; do
let window++
tmux new-window -n $fn -t $CLUSTER_NAME
SERVER=$fn
eval `eval "echo tmux send-keys -t $CLUSTER_NAME:$window.1 \'$CONNECT_STRING\' C-m"`
done
fi
tmux attach -t $CLUSTER_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment