Is there any way to save a tmux session? In other words, if I reboot the computer, will I always lose the sessions?
6 Answers
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.
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.
- 2,230
-
-
2This works for mutltiple tmux sessions, but it does not save panes within each window. – SimplyKnownAsG Dec 05 '19 at 17:46
-
1Your
tmux-sessionscript is now in my dotfiles project. Thanks! https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles – Gabriel Staples Mar 22 '20 at 06:48 -
many thanks for the script @mislav. I saved the script as .zsh and ran
~/.tmux-session.zsh savein the terminal without errors. Then restarted the computer and then open tmux interminal and ran~/.tmux-session.zsh restoreand gotwidth invalidas a message (with one additional window created in main directory. But the saved session (with 3 windows and 2 panes in each) was not reloaded. Do you have an idea of what could have gone wrong? – ecjb Aug 10 '20 at 12:01 -
I just changed the extension from
~/.tmux-session.zshto~/.tmux-sessionand got a serie of error messages~/.tmux-session: line x: y: command not found(wherexis between 0 and 27 andyis between 0 and 2 (for 2 window, one with 2 panes, the other with one pane) – ecjb Aug 10 '20 at 12:07 -
This was very useful. Thanks! The tmux save format looks to have changed, so I made a minor change to the restore script. – hojusaram Apr 27 '21 at 01:32
-
1@mislav you've had a TODO in that file since 2013. May I recommend https://todoist.com – abbood May 14 '22 at 05:51
-
It saves the session tabs and directories but that's it. It's a good start though. – Lubo Kanev Jul 14 '22 at 08:02
I wrote a tmux plugin that enables you to save complete tmux environment and restore it later. It strives to be really detailed so that you have a feeling you never quit tmux.
https://github.com/tmux-plugins/tmux-resurrect
Update: now there's a tmux-continuum plugin that performs automatic background saves of tmux environment. Optionally it also *automatically* restores tmux env after computer reboot.
-
This plugin is not bad, but it did not restore all my programs. Will read more of your docs and maybe submit an issue on github. – Arne Jul 31 '16 at 12:05
-
@Arne Depending on the program, this may require program checkpointing. Instead, I would recommend configuring your programs to restore - persistent .vimrc files and cursor positions for vim, etc. - and storing the tmux pane_current_command for programs like man that can be re-opened. Checkpointing is very complicated in my opinion, but worth looking into in any case. – John P Apr 03 '17 at 01:52
-
4@bruno-sutic whats the diffrence between your plugin (tmux-resurrect) and tmux-coninuum? – lony Aug 22 '17 at 07:38
-
Currently I needed to patch continuum like this to make it work https://github.com/tmux-plugins/tmux-continuum/blob/master/docs/continuum_status.md – rofrol Dec 21 '18 at 09:51
-
I had some problems with bash commands history reloading (when using set -g @resurrect-save-shell-history 'on') and generally with history persistence when switching among terminals in tmux. See this thread and especially its 5th comment to see a workaround (or hack) I successfully used to overcome this problem – Matej Vargovčík Apr 10 '19 at 07:26
-
When using my workaround script I also suggest modifying tmux-resurrect plugin by disabling saving of bash history (only loading should be enabled, my script handles history saving after each command). I.e. in current version of tmux-resurrect you should delete lines 299-301
if save_shell_history_option_on; then ; dump_shell_history ; fiin tmux-resurrect/scripts/save.sh. The history works without modifying the plugin too but the history files get large and unclean after several tmux environment saves (and can cause performance issues because my script reloads history after each command). – Matej Vargovčík Apr 10 '19 at 08:31 -
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.
- 331
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.
-
26
-
1@cpast: This is true, but comments can also rot. Best to give both :) – danielpops Apr 21 '16 at 16:44
-
7
-
2Here's a related github link from that author: https://github.com/edsantiago/tmux-session – zeroimpl Jun 06 '20 at 02:06
I successfully use https://github.com/jimeh/tmuxifier for recreating sessions. This can be installed without ruby, just using git.
Samples are pretty self explanatory, e.g.: https://github.com/jimeh/tmuxifier/blob/master/examples/example.session.sh
- 241
tmux send-keys ...line? – Dominykas Mostauskis Apr 15 '14 at 13:38
– Criminally Inane Mar 26 '19 at 22:49alias sshlocal='ssh myname@localhost -t "tmux new-session -A -s main"'