Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

migrated from stackoverflow.com Jun 22 '12 at 3:01

This question came from our site for professional and enthusiast programmers.

    
You can hibernate to disk as an alternative –  Matt Freeman - nonuby Jun 23 '12 at 5:33
19  
Hibernating is an alternative to leaving your computer running constantly, not rebooting. –  chepner Jun 23 '12 at 17:31

5 Answers 5

up vote 67 down vote accepted

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.

share|improve this answer
    
doesn't sound like that will do anything if I want to restore a session with 5 files open. No way to do that? –  chovy Jan 25 '13 at 1:38
    
Tmux 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 Feb 2 '13 at 14:44
    
What is the purpose of the tmux send-keys ... line? –  Dominykas Mostauskis Apr 15 at 13:38
    
@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 Apr 16 at 15:47

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.

share|improve this answer

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

share|improve this answer
    
Just as I posted my "answer" with the same link I looked up - "do'h!" Found this while trying to remember what your plugin was called. –  phatskat Sep 8 at 13:20

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.

share|improve this answer
10  
Could you please post what they say? Links can rot. –  cpast Feb 2 '13 at 22:30

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.

share|improve this answer

protected by Community Sep 8 at 13:19

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.