The Computer Oracle

Restore tmux session after reboot

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3

--

Chapters
00:00 Question
00:20 Accepted answer (Score 233)
02:15 Answer 2 (Score 167)
02:52 Answer 3 (Score 140)
03:24 Answer 4 (Score 12)
03:44 Thank you

--

Full question
https://superuser.com/questions/440015/r...

Accepted answer links:
[Tmuxinator]: https://github.com/aziz/tmuxinator
[Teamocil]: https://github.com/remiprev/teamocil

Answer 2 links:
[I wrote a simple bash script]: https://github.com/mislav/dotfiles/blob/...

Answer 3 links:
https://github.com/tmux-plugins/tmux-res...
[tmux-continuum]: https://github.com/tmux-plugins/tmux-con...

Answer 4 links:
[tmuxinator]: https://github.com/tmuxinator/tmuxinator

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#tmux

#avk47



ACCEPTED ANSWER

Score 244


Yes, if you reboot you computer you will lose the sessions. Sessions cannot be saved. But, they can be scripted. What most do in fact is to script some sessions so that you can re-create them. For instance, here's a trivial shell script to create a session:

#!/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

Here's what it does. First, it checks if there's any session already with that name (in this case, the very original name is "script") with tmux has-session. It checks the return code. If there's a ongoing session with that name already, it skips the "if" cycle and go straight to the last line, where it attaches to the session. Otherwise, it creates a session and sends some keys to it (just running a random script for now). Then it exits the "if" block and attaches.

This is a very trivial sample. You can create multiple windows, panes, and the like before you attach.

This will not be the very same thing you asked for, though. If you do any changes to the session, for instance you rename a window and create a new pane in it, if you reboot those changes won't of course be saved.

There are some tools that ease the process of scripting sessions, although I prefer to do things manually (I think it is more versatile). Those tools are Tmuxinator and Teamocil.

My main source of informations was "The Pragmatic Bookshelf" Tmux book.




ANSWER 2

Score 179


I wrote a simple bash script that persists open tmux sessions, windows and current working directories in each.

Call it like so manually or periodically from cron (because you might forget):

tmux-session save

It will write to ~/.tmux-session. Restore them after reboot like so:

tmux-session restore

I find this much better than a several hundred line long Perl script.




ANSWER 3

Score 12


tmuxinator is a tool written in Ruby, that could be used to create and manage tmux sessions with ease. It could be used to create a project, which could later be instantiated as as tmux session.




ANSWER 4

Score 5


Consider this partial solution found here

The author creates a function that saves the history of the tmux session in order to persist the state of the tmux session post a server reboot.