How to automate your tmux sessions using simple shell script

Automate Tmux with Shell Script

The script that I used very often when I have to develop/debug Rails app away from my big screen.

My personal usecases:

Basic Usage

I usually start the session by calling this script directly

Then to use it just issue the command like

# start the new session named `rails-dev`
~/bin/t-start rails-dev

Once you are finish with the session you can kill it with

# kill the session you create earlier with command like
tmux kill-session -t rails-dev

Please take a look at the script and modify to your specific projects.

The source code for script

#!/bin/bash

## Kill existing process based on simple grep/awk
function kill_process() {
  ps aux | grep -i "$1" | grep -v grep | awk '{print $2}' | xargs kill -9 2> /dev/null;
}

## Set the initial environment
function set_base_environment() {
  # This has to be explicitly set in tmux for some reason!
  export PATH=$PATH

  # Set the global session
  export SESSION_NAME=$1
}

## Wrapper function to kill existing tmux session
function kill-session() {
  tmux has-session -t $g_session_name 2> /dev/null
  if [ "$?" -eq 0 ]
  then
    tmux kill-session -t $g_session_name
  fi
}

## Create the new tmux session with a given name
function create-session() {
  window_name=$1
  init_command=$2
  tmux new-session -s $g_session_name -n "$window_name" -d
  tmux send-keys   -t $g_session_name:1  "$init_command" C-m;
  inc-session
}

## Add a single window to existing session
function add-window() {
  inc-session
  window_name=$1
  init_command=$2
  tmux new-window -t $g_session_name:$g_session_count -n "$window_name"
  tmux send-keys  -t $g_session_name:$g_session_count "$init_command" C-m;
}

## List session information
function session-info() {
  echo "Your session information"
  echo $g_session_count
  tmux ls
}

## Attach to existing specific session if any
function attach-session() {
  tmux attach-session -t $g_session_name
}

## Increment the global g_session_count
function inc-session() {
  ((g_session_count++))
  tmux set-environment -t $g_session_name g_session_count $g_session_count
}

## Main Program {{{
g_session_name=$1
# Use the default value if not specified
: ${g_session_name:="rails-dev"}
g_session_count=0

## Kill the existing process if any
kill_process 'sidekiq'
kill_process 'bin/rails'
kill_process 'redis-server'

## Set the session name
set_base_environment $g_session_name

## Kill existing session if any
kill-session $g_session_name

## Create new session, add any windows as required
create-session 'redis-server' 'cd ~/codes/exec-io/envisage; redis-server'
add-window 'sidekiq' 'cd ~/codes/exec-io/envisage; bundle exec sidekiq'
add-window 'rails-console' 'cd ~/codes/exec-io/envisage; rails console -s'
add-window 'rails-server' 'cd ~/codes/exec-io/envisage; rails server'
add-window 'guard' 'cd ~/codes/exec-io/envisage; bundle exec guard'
add-window 'editor''cd ~/codes/exec-io/envisage; vim'

session-info
attach-session
## }}}
comments powered by Disqus