Skip to main content
DARKPC.NET

Rebuild a Named Tmux Workspace After a Reboot

Create a small tmux launcher that restores a named 2×2 workspace from scratch and attaches to an existing session on repeat use.

Difficulty Intermediate

Prerequisites

Use a shell account with tmux on Kali or another Linux system. Run the exercise from a terminal outside tmux. The workspace layout is recreated after reboot. A live pane's unsaved editor state, command history and running processes need their own persistence mechanisms.

Create a workspace launcher

Save the following in your home directory as darkpc-workspace.sh. The commands give the first window a reproducible starting directory using your own home folder.

#!/bin/sh
set -eu

session=darkPC
if [ -n "${TMUX:-}" ]; then
    printf '%s\n' 'Run this from outside tmux.' >&2
    exit 1
fi

if ! tmux has-session -t "=$session" 2>/dev/null; then
    first_pane=$(tmux new-session -d -P -F '#{pane_id}' \
        -s "$session" -n work -c "$HOME")
    right_pane=$(tmux split-window -h -P -F '#{pane_id}' -t "$first_pane" -c "$HOME")
    tmux split-window -v -t "$first_pane" -c "$HOME"
    tmux split-window -v -t "$right_pane" -c "$HOME"
    tmux select-layout -t "=$session:work" tiled
    tmux select-pane -t "$first_pane"
fi

exec tmux attach-session -t "=$session"

The launcher captures the right-hand pane ID so it can split each column explicitly. The final tiled layout arranges four pane IDs in a grid. The file only needs read permissions if invoked with sh, so run sh "$HOME/darkpc-workspace.sh". Detach with Ctrl+B then D and execute the same command again. It should attach to the same workspace. The exact session target prevents a similarly named session from being selected.

Troubleshooting and practice

A very small terminal can make a split fail with no space for new pane. Enlarge the terminal first. If you interrupted the launcher before it finished, inspect the session using tmux list-panes -t darkPC:work and finish arranging it manually. Use a custom session name for a practice workspace if darkPC is already in use. To verify the layout, run tmux list-panes -t darkPC:work -F '#{pane_id} #{pane_width}x#{pane_height}'. Avoid placing credentials, commands that change files or network jobs into a reusable layout launcher. Open a second terminal, attach again and observe that both clients see the same panes.

References and related reading