DEV Community

Serhat Teker
Serhat Teker

Posted on Originally published at tech.serhatteker.com on

Restore Tmux Sessions After Reboot

OK, we see how we save and restore our sessions in tmux. Look at this article: Restore Tmux Sessions

But after rebooting the system how can we start the saved sessions? Here what I was doing before:

  • To restore sessions after reboot I run:
  $ tmux new-session
Enter fullscreen mode Exit fullscreen mode
  • Then I hit prefix + ctrl-r

This restores all of my sessions and windows, but it also leaves me with the
"extra" session that was created with new-session.

  • I kill this new session. This drops me out of tmux and back to the default terminal. Then I have to run:
  $ tmux attach
Enter fullscreen mode Exit fullscreen mode

Now I'm finally in my restored session without the "extra new-session".

You see it is a real pain. For every reboot. For. Every. Reboot.

For lazy developers like us, there is always a "one-line" or "one-click" or "one command" solution. Thanks for github there were some alternatives and found mine: github solution.

alias mux='pgrep -vxq tmux && tmux new -d -s delete-me && tmux run-shell ~/.tmux/plugins/tmux-resurrect/scripts/restore.sh && tmux kill-session -t delete-me && tmux attach || tmux attach'
Enter fullscreen mode Exit fullscreen mode

I had to tweak this code a little bit since -q flag throws an error. Also I
made it more readable.

alias mux='pgrep -vx tmux > /dev/null && \
        tmux new -d -s delete-me && \
        tmux run-shell ~/.tmux/plugins/tmux-resurrect/scripts/restore.sh && \
        tmux kill-session -t delete-me && \
        tmux attach || tmux attach'

Enter fullscreen mode Exit fullscreen mode

OK, now it is a "one command" solution.

Top comments (1)

Collapse
 
saltgen profile image
Sagnik Dutta • Edited

Hello Serhat, good stuff!
However I am getting an error when I use "mux" as command.
"~/.../tmux-resurrect/scripts/restore.sh" returned 127

I am using Fish shell, not sure if that's an additional problem for tmux.