Restore tmux session after reboot

277

140

Is there any way to save a tmux session? In other words, if I reboot the computer, will I always lose the sessions?

KendallB

Posted 2012-06-21T04:17:49.010

Reputation: 2 881

6@chepner: There are those days when *** System restart required *** – karatedog – 2015-12-07T22:11:19.937

3@karatedog Yes, so we both agree that hibernation is not a solution to losing your tmux session on reboot. – chepner – 2015-12-07T22:21:56.703

1You can hibernate to disk as an alternative – Mâtt Frëëman – 2012-06-23T05:33:48.327

37Hibernating is an alternative to leaving your computer running constantly, not rebooting. – chepner – 2012-06-23T17:31:46.487

Answers

176

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.

Dakatine

Posted 2012-06-21T04:17:49.010

Reputation: 2 120

can I have the script do ssh login with username and pass? (I know its not secured just want to know if its possible for systems where i don't care about seurity but still have to have user and pass). – Jas – 2016-08-11T05:12:20.637

@jas I use key pairs only (passwordless login) and put an alias like this in my .bashrc to SSH to my local VM. Then I just type 'sshlocal' and I'm in:

alias sshlocal='ssh myname@localhost -t "tmux new-session -A -s main"' – Criminally Inane – 2019-03-26T22:49:02.430

1doesn't sound like that will do anything if I want to restore a session with 5 files open. No way to do that? – chovy – 2013-01-25T01:38:26.023

5Tmux doesn't know anything about the state of processes you may have had running. You could script having the same files open by having the 'send-keys' or 'split-window' command be 'vim file1 file2 file3' or look into your editor's session management (vim -S and the like) – bloy – 2013-02-02T14:44:16.707

What is the purpose of the tmux send-keys ... line? – Dominykas Mostauskis – 2014-04-15T13:38:48.217

2@DominykasMostauskis that command sends key presses to the specified session. It's like entering the session, and inputing those very keys from the keyboard. In this case, you send "~/bin/script" followed by Enter. – Dakatine – 2014-04-16T15:47:28.070

122

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.

mislav

Posted 2012-06-21T04:17:49.010

Reputation: 1 670

Is this only if you have one tmux session though? – Sevenearths – 2019-05-17T09:36:32.253

This works for mutltiple tmux sessions, but it does not save panes within each window. – SimplyKnownAsG – 2019-12-05T17:46:26.773

92

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.

user124460

Posted 2012-06-21T04:17:49.010

Reputation:

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 – 2016-07-31T12:05:29.957

@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 – 2017-04-03T01:52:01.847

1@bruno-sutic whats the diffrence between your plugin (tmux-resurrect) and tmux-coninuum? – lony – 2017-08-22T07:38:55.963

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 – 2018-12-21T09:51:25.900

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 – 2019-04-10T07:26:52.530

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 ; fi in 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 – 2019-04-10T08:31:08.070

10

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.

Praveen Kumar

Posted 2012-06-21T04:17:49.010

Reputation: 271

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.

greg

Posted 2012-06-21T04:17:49.010

Reputation: 51

1@cpast: This is true, but comments can also rot. Best to give both :) – danielpops – 2016-04-21T16:44:46.757

6@greg Your link returns 404 – rofrol – 2018-12-21T09:50:14.960

17Could you please post what they say? Links can rot. – cpast – 2013-02-02T22:30:09.933

1

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

stej

Posted 2012-06-21T04:17:49.010

Reputation: 201