GeekCoding101 make your way to geek

Bash Script to Rebuild a tmux Dev Environment After Reboot

tmux-resurrect restores windows, not running services. Here's the script I use to rebuild a six-window Django, Celery, Vue tmux session after reboot.

6 min read
Crafting A Bash Script With Tmux 1
On this pageWhat you need

tmux-resurrect and tmux-continuum can bring back my tmux windows after a reboot, but not the services and environment variables running inside them. So I wrote two small Bash scripts that recreate my six-window Django/Vue session and start every service in the right window with one command.

Below is the setup, why the plugins weren’t enough, and both scripts, which you can adapt to your own project.

What you need

  • tmux
  • A local Django + Celery + Vue project (the same pattern works for any stack)
  • Conda for the Python environment, Homebrew services for mailpit and redis, and a local PostgreSQL. Swap in whatever you use.

The background: my six tmux windows

I have a Django/Vue development environment running locally. To streamline development, I open six tmux windows in one tmux session 😎:

WindowWhat it does
celeryChecks and starts local services (mailpit, redis), then starts the Celery worker
flowerStarts Flower to monitor Celery
djangoRuns migrations, then runserver
django-managerA Django manage.py shell for manager operations
herokuChecking Heroku status, commits and other Heroku operations
nvm (Vue)npm run serve or build for the Vue frontend

My laptop sometimes needs to reboot, and after a reboot all of these windows are gone 😓

The problem: tmux-resurrect restores windows, not services

Typically, my development windows look like this, with each service running in its own window:

six tmux windows with services running

I have tmux-resurrect and tmux-continuum configured. After a reboot they do restore the saved windows correctly. The services and all the environment variables, however, are gone.

To simulate a reboot, I killed all tmux sessions:

killing all tmux sessions

Then I started tmux again. tmux restored the previously saved windows:

restored session status

Let’s check a window:

restored window with nothing running

None of the services is running 🙉

The complaint

As the supreme overlord of geekcoding101.com, I simply cannot let such imperfection slide. Not on my watch. Nope, not happening. This ain’t it, chief.

Okay, let’s fix it!

The fix: two scripts

I wrote a script… oh no! Two scripts!

  1. start_tmux_dev_env.sh creates the session and all six windows. In each window, it sources the second script and calls one function.
  2. prepare_dev_env.sh defines one setup_*_window function per window. Each function activates the environment, exports the variables that window needs and starts the service.

Sourcing a file of functions inside each window means the environment variables live in that window’s shell, exactly where the service needs them.

start_tmux_dev_env.sh

Replace myproject and /path/to/myproject with your own names and paths.

#!/bin/bash
# Please note: don't use a dot in the session name.
SESSION_NAME="myproject"
PROJECT_DIR="/path/to/myproject"
PREPARE="$PROJECT_DIR/utils/prepare_dev_env.sh"
# Only build the session if it doesn't exist yet
tmux has-session -t "$SESSION_NAME" 2>/dev/null
if [ $? != 0 ]; then
# Create a new detached tmux session
tmux new-session -d -s "$SESSION_NAME"
# --- celery window ---
tmux rename-window -t "$SESSION_NAME" 'celery'
echo "Starting celery window..."
echo "Sleeping 5s to wait for window celery to finish initialization....."
sleep 5
echo "Checking psql in window celery..."
tmux send-keys -t "$SESSION_NAME:celery" 'psql -h localhost -p 5432 -d myproject' C-m
sleep 2
tmux send-keys -t "$SESSION_NAME:celery" '\q' C-m
sleep 1
# Put this in the background, otherwise the script waits here forever.
tmux send-keys -t "$SESSION_NAME:celery" ". $PREPARE && setup_celery_window" C-m &
# --- flower window ---
echo "Starting flower window..."
tmux new-window -t "$SESSION_NAME" -n 'flower'
echo "Sleeping 5s to wait for window flower to finish initialization....."
sleep 5
# $SESSION_NAME:flower targets the window; $SESSION_NAME.flower would target a pane
tmux send-keys -t "$SESSION_NAME:flower" ". $PREPARE && setup_flower_window" C-m &
# --- nvm (Vue) window ---
echo "Starting nvm window..."
tmux new-window -t "$SESSION_NAME" -n 'nvm'
sleep 5
tmux send-keys -t "$SESSION_NAME:nvm" ". $PREPARE && setup_nvm_window" C-m &
# --- django window ---
echo "Starting Django window..."
tmux new-window -t "$SESSION_NAME" -n 'django'
sleep 5
tmux send-keys -t "$SESSION_NAME:django" ". $PREPARE && setup_django_window" C-m &
# --- django manager window ---
echo "Starting Django manager window..."
tmux new-window -t "$SESSION_NAME" -n 'django-manager'
sleep 5
tmux send-keys -t "$SESSION_NAME:django-manager" ". $PREPARE && setup_django_manager_window" C-m &
# --- heroku window ---
echo "Starting Heroku window..."
tmux new-window -t "$SESSION_NAME" -n 'heroku'
sleep 5
tmux send-keys -t "$SESSION_NAME:heroku" "cd $PROJECT_DIR" C-m
fi
# Attach to the tmux session
tmux attach -t "$SESSION_NAME"

A few notes on the design:

  • Guarding with tmux has-session means running the script twice just re-attaches. It won’t create a duplicate session.
  • The sleep 5 before each send-keys gives the new window’s shell time to finish loading your shell config (conda init and so on) before keys arrive.
  • The nvm window starts before the django window because setup_django_window waits for npm to come up.

prepare_dev_env.sh

This file is sourced, not executed, so its functions run inside each window’s own shell.

#!/bin/sh
WORKDIR="/path/to/myproject"
CONDA_ENV="myproject.django"
setup_celery_window() {
conda activate ${CONDA_ENV}
cd $WORKDIR
brew services start mailpit
brew services start redis
export REDIS_URL=redis://localhost:6379/0
export USE_DOCKER=no
export CELERY_CONFIG_TASK_ALWAYS_EAGER=yes
celery -A config.celery_app worker --loglevel=info
}
setup_flower_window() {
conda activate ${CONDA_ENV}
cd $WORKDIR
export REDIS_URL=redis://localhost:6379/0
export CELERY_BROKER_URL=$REDIS_URL
# Local-only credentials; never reuse these anywhere real.
export CELERY_FLOWER_USER=debug
export CELERY_FLOWER_PASSWORD=debug
export USE_DOCKER=no
export CELERY_CONFIG_TASK_ALWAYS_EAGER=yes
celery -A config.celery_app -b ${CELERY_BROKER_URL} flower --basic_auth="${CELERY_FLOWER_USER}:${CELERY_FLOWER_PASSWORD}"
}
setup_django_window() {
conda activate ${CONDA_ENV}
cd $WORKDIR
export REDIS_URL=redis://localhost:6379/0
export CELERY_BROKER_URL=$REDIS_URL
export CELERY_FLOWER_USER=debug
export CELERY_FLOWER_PASSWORD=debug
export USE_DOCKER=no
export EMAIL_HOST=localhost
export CELERY_CONFIG_TASK_ALWAYS_EAGER=yes
# Please export sensitive information manually, like your OpenAI key.
# Replace ${DB_USERNAME} here if you haven't set it as an environment variable.
export DATABASE_URL=postgres://${DB_USERNAME}@127.0.0.1:5432/myproject
python manage.py migrate
echo "Sleeping 10s to wait for npm to start in another window..."
sleep 10
python manage.py runserver 0.0.0.0:8000
}
setup_django_manager_window() {
conda activate ${CONDA_ENV}
cd $WORKDIR
export USE_DOCKER=no
# Replace ${DB_USERNAME} here if you haven't set it as an environment variable.
export DATABASE_URL=postgres://${DB_USERNAME}@127.0.0.1:5432/myproject
export REDIS_URL=redis://localhost:6379/0
export CELERY_CONFIG_TASK_ALWAYS_EAGER=yes
python manage.py shell
}
setup_nvm_window() {
conda activate ${CONDA_ENV}
cd $WORKDIR/frontend
npm run serve
}

Keep secrets such as API keys out of this file. Export them by hand in the window that needs them.

The end

Now, after a reboot, I just run start_tmux_dev_env.sh and it spins up all the windows, with services running, in seconds!

I’m Really Proud Of You GIF from Ron Swanson GIFs

I recorded a video of the script running. Check it out in my post Terminal Mastery: Crafting A Productivity Environment With iTerm, Tmux, And Beyond.

Terminal Mastery: Crafting A Productivity Environment With iTerm, tmux, And Beyond

FAQ

Why doesn’t tmux-resurrect restart my services after a reboot? tmux-resurrect saves and restores the layout: sessions, windows, panes and working directories. It can re-launch a configured list of programs. It does not bring back shell state such as exported environment variables or an activated conda environment, so services that depend on them don’t come back correctly.

Do I still need tmux-resurrect and tmux-continuum with this script? Not for this session, because the script rebuilds it from scratch. You can keep the plugins for other sessions you don’t script.

Why use two scripts instead of one? The launcher only creates windows and sends keys. The per-window setup lives in functions you source in each window, so each window gets its own environment variables. You can also re-run a single function, like setup_django_window, by hand without rebuilding the whole session.

What happens if I run the script when the session already exists? tmux has-session detects it, the script skips the setup and simply attaches to the existing session.

Can I use this without Django or conda? Yes. Keep the structure: one new-window plus one send-keys per window, and one setup function per window. Replace the commands inside each function with your own stack.

Thanks for reading my blog post! Feel free to check out my other posts or contact me via the social links in the footer.

Keep reading

All posts