Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: Restore tmux environment after a system restart (github.com/tmux-plugins)
241 points by brunosutic on Aug 30, 2014 | hide | past | favorite | 43 comments



I also really love tmuxp, which lets you save tmux sessions as json files and save/reload them later. Great for setting up tmux sessions for different workflows.

http://tmuxp.readthedocs.org/


Holy. Wow. This is the most exciting thing that I've seen this weekend. I've created hacks on hacks to get around this problem. I'm SO happy about this! Thank you!


I've visited HN for a long time and this is the first submission I can say of:

I need this in my life.

Thanks


Great! Now can someone tell me how to configure tmux so that I can have two connected term emulators use multiple virtual consoles in one session? Screen allows you to do this and I've been searching for a way to get tmux to behave the same way.

(In fact, I've had a real problem getting tmux to behave well at all when multiple term emulators are connected to the same session. I've had weird window resizing problems.)


Shameless plug: https://github.com/bsandrow/tmux.sh

I wrote that in response to the last thread complaining about said feature came up.


well done. i'll have a go at making byobu execute this script instead of tmux itself to see if that fixes things.


If I undertstand you correctly:

tmux new-session -t <existing_session_name>

(Do a "tmux ls" to see existing sessions)

The one problem with this approach is that each session created this way (i.e. one that "attaches" to an existing session) actually creates a new "mirror" (?) session that will eventually clutter up your list of existing sessions.

My workaround is to create an initial named session (tmux new-session -s name), then attach to that named session. I never use "tmux ls" :p

You may also want to add "set-window-option -g aggressive-resize on" to your .tmux.conf (or prepend with "tmux" and run it in an existing session). To quote the manpage: "will resize the window to the size of the smallest session for which it is the current window, rather than the smallest session to which it is attached".


From the 2nd, 3rd, etc terminal you can see the list of sessions with 'tmux ls'. After that, attach to one of them with 'tmux a -t [session-number]'.


One workaround is to detach the other sessions when you connect.

tmux a -d


The bottom of this page seems to deal with this issue: http://mutelight.org/practical-tmux


BTW do people use tmux/screen to start multiple apps for development?

I'm looking for a way to start nginx/sass watcher/js builder/web app in a split screen, and also stop them all with one command (which would send ctrl-c to all windows). Now I just use tabs (OSX).


Check out tmuxinator: https://github.com/tmuxinator/tmuxinator. You can create configurations for your projects that specify windows, panes, which apps to run, etc. It's great for starting all the servers/shells/workers required for a project.


This is the use case of Foreman. https://github.com/ddollar/foreman



Thanks for all replies. I tested this before the tmux solution, and I must say I'm impressed.

Ctrl-c worked like I was running a single app:

    16:44:33    scss | QUIT
    16:44:33 webpack | QUIT
    16:44:33   nginx | QUIT
    16:44:33      go | QUIT
This was better than expected! I suppose tmux will be preferable if one of the apps is slow to start and must be killed individually.



After using Goreman for a few days, I miss a way to restart a single process. Maybe just typing that process number should restart it... do you know if any of the solution do this?


I just use a shell script based on this SO answer. https://stackoverflow.com/questions/9023164/in-bash-how-can-...

The only sort of problem with it is that the output gets merged with each other. You can quite easily figure out what is what though.


See the goreman solution below: its similar but each output line has its app prefixed.


I find teamocil[1] for this use case, usually with one window opening a vimsplit of TODO, README, CHANGELOG etc, one window to launch a vagrant machine (which then runs all the daemons), another for guard/rspec output, and other windows for key files currently being worked on.

1: https://github.com/remiprev/teamocil


I use tmuxinator to launch services I need, then I just kill the session when I'm done.

https://github.com/tmuxinator/tmuxinator


yes.. http://devstack.org for openstack, does a ~20 window screen session with all the servers attached to the foreground.


Wow, this is great! I've gone weeks without software updates because I didn't want to lose my tmux session.


I just installed it and played around with it for a bit - it's a really nice - but what I would kill for is something that would restore tmux server state - including the various scroll-back-buffers.

One of the few reasons I still drop out of tmux, and switch over to Terminal.app (iTerm2 might do this as well natively) - is when you have a system crash or Kernel Panic (which happens all to often with OS X -weekly basis, or daily if I'm plugging/unplugging the USB -> Serial adapters), and OS X reboots - Terminal.app restores all of my history buffers - so I don't lose any of the work in my screens.

Yes, I know script is a good way of doing this - but When you are opening a dozen or so windows, it's nice to be able to just scroll back in your history to see what you are working on.

Saving the scroll-back buffer is possible, (https://github.com/tmux-plugins/tmux-logging) - I just don't know if there is any way of restoring it.

Even if I could just do this prior to doing something dangerous (like unplugging a USB cable), that would be great.

I just noticed, that both of these plugins are by the same developer, brunosutic, - so perhaps we'll see this feature in the future. If so, super excited! :-)



I thought session restore for tmux was awesome, and then I got to know that tmux has a plugin manager and fuzzy history search! brunosutic, you made my day.


Great script and timing. For those who missed it, this was just requested on HN yesterday https://news.ycombinator.com/item?id=8243157

Screencast is a nice touch.


Tangentially, anyone know how to run tmux (or screen) so that SSH sessions automatically attach?

Ie., I want every interactive connection to automatically attach to a session on the server if one exists, otherwise create one, so that when I disconnect or quit, my shell stays.

I tried to hack a solution using .zshrc a while ago, but it just didn't work properly and/or didn't handle all the edge cases, I forget exactly.


I have this in my .kshrc, can't recall where I found it.

  if [ "$PS1" != "" -a "${STARTED_TMUX:-x}" = x -a "${SSH_TTY:-x}" != x ]
  then
      STARTED_TMUX=1; export STARTED_TMUX
      sleep 1
      ( (tmux has-session -t remote && tmux attach-session -t remote) || (tmux new-session -s remote) ) && exit 0
      echo "tmux failed to start"
  fi

I also don't recall why the "sleep" command is in there.

Edit: to clarify, this is in the .kshrc of my work machine. When I start xterms on that machine, they just get a normal shell because $SSH_TTY is not set. When I ssh into my work machine from elsewhere, I get attached to the tmux session (if any) or one is started.


What is the point of this:

  "${STARTED_TMUX:-x}" = x
vs.

  -z "$STARTED_TMUX"
or

  "$STARTED_TMUX" = ""
It's always seemed like a legacy method of testing to empty string from a time when "" wouldn't be parsed as an argument. Is there any real reason to still do that today?


> I also don't recall why the "sleep" command is in there.

Probably this issue? http://superuser.com/questions/629227/tmux-prints-12c-whenev...


Thanks, I'll play around with this.


The 'tmux' plugin for oh-my-zsh (https://github.com/robbyrussell/oh-my-zsh) does this. If you want to avoid bloat you could just extract the script.

EDIT: though I believe you'd need to have that on the server side.


This looks like a cool project, although I personally prefer separate tmux sessions for each project.

Also, am I missing something, or does this really execute the last command for each pane/window which had a process other than shell running? I can imagine a few potentially dangerous situations if this is how it works.


It pretty clearly says it only runs a very small subset of commands.

"Only a conservative list of programs is restored by default: vi vim emacs man less more tail top htop irssi."


Yes, and for the paraniod there's an option that disables restoring any command.


Thanks! I've been meaning to use tmux more and this will make it all the easier.


This looks really cool. There are a lot of people (like me) who live in tmux and I'm really surprised this hadn't been done earlier.


Cool stuff. But doesn't work for me. It's not saving when I press prefix+Alt+s. Anyone with the same experience?

I'm on MBP, tmux 1.9a.


Some terminal emulators are pretty weird about sending the Alt key. If you open vim, in insert mode, and type <C-v><Alt-s>, it'll show you what is keys are actually being sent. If it's not what you expect, try remapping it to something other than <Alt-s>.


Sorry to hear that, can you please open a github issue for this?


I was just looking for something like this a couple of weeks ago. Thanks for sharing!


sidenote: your terminal color setup is rad.

edit: I'd also like to say, I am really enjoying the trend of people including a video demo of their applications.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: