Home server tmux startup script

I use a thin client as a home server to host several services on my local network. I don’t want to mess much with Linux config files, so thusfar I’ve simply started each of these services manually—at first via GUI, and later headlessly with tmux.

When I reboot the device, it gets annoying having to type each command to get up and running again. Thanks to Ryan Himmelwright’s “Scripting A Tmux Work-space Start-up” I was able to put together a simple Bash script to semi-automate this.

First I create a session with tmux new-session, using -d to start it in the background. To make subsequent commands more explicit, I give it a friendly name with -s

      tmux new-session -d -s "MySession"

    

Next I create a new-window for each process I want to run.

      tmux new-window -t "MySession" -n "MyWindow"

    

Finally I use send-keys to start each process. I read that you can instead provide the desired command after new-window; However I had inconsisent results with this—it seemed to silently fail on multi-part commands, particularly those that involve changing directories. Note that with send-keys you need to include Enter or C-m after each line to submit it to the terminal.

      tmux send-keys -t MySession:MyWindow "echo hello world" Enter

    

Here’s a complete startup.sh script that runs a couple sample processes.

      #!/bin/bash

SESSION="home-server"

# setup session

tmux new-session -d -s $SESSION

# setup windows

tmux new-window -t $SESSION -n "homeassistant"
tmux new-window -t $SESSION -n "qbittorent"

# start processes

tmux send-keys -t $SESSION:homeassistant "cd ~/HomeAssistant && docker compose up" Enter
tmux send-keys -t $SESSION:qbittorent "qbittorent-nox" Enter