DEV Community

Serhat Teker
Serhat Teker

Posted on • Originally published at tech.serhatteker.com on

Restore Tmux Sessions

tmux is a great tool for "black panel geeks" like me.

For those who don't know tmux, it is a
terminal multiplexer for Unix-like operating systems. It enables a number of terminals to be created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached.

It is really great for handling terminal sessions whether local or remote.

My pain was with tmux when I reboot the system my sessions gone. I coded and tried many scripts that I found like below but none of those worked as I expected.

#!/bin/zsh

SESSIONNAME="script"
tmux has-session -t $SESSIONNAME &> /dev/null

if [ $? != 0 ]
 then
    tmux new-session -s $SESSIONNAME -n script -d
    tmux send-keys -t $SESSIONNAME "~/bin/script" C-m
fi

tmux attach -t $SESSIONNAME
Enter fullscreen mode Exit fullscreen mode

Finally found a solution for this pain: tmux-resurrect.

tmux-resurrect

tmux-resurrect plugin enables saving and restoring tmux sessions manually.
Even if you like to go one step further there is another plugin
tmux-continuum. It saves sessions automatically. However I am more comfortable with manual savings.

Requirements

Setup

Add plugin to the list of TPM plugins in .tmux.conf:

set -g @plugin 'tmux-plugins/tmux-resurrect'
Enter fullscreen mode Exit fullscreen mode

And Hit prefix + I to fetch the plugin and source it.

Use

You can save sessions with prefix + Ctrl + s and restore with prefix + Ctrl + r. For sure you can always map these binding to another keys.

All done!

Top comments (0)