Bash – Why might tmux only be capable of attaching once per shell session

bashshelltmux

I'm using tmux on a Fedora 8 server (workplace restriction) to monitor several boxes on a stress rack. I noticed that I can only attach to my detached tmux session once per shell session. In order to reattach, I must kill the terminal session and start another one, otherwise my attach command simply hangs (although, Ctrlb,d detaches from the hung command).

Has anyone seen behavior like this before? Any guidance or pointers to references would be greatly appreciated.

Best Answer

In my Shell's config file (I use zsh, so its in my .zshrc, should work in a .bashrc) I have the following:

# Default to TMUX
if [ -z "$TMUX" ]; then
  base_session=$USER"_session"
  # Create the base session if it doesn't exist
  tmux has-session -t $base_session || tmux new-session -d -s $base_session
  # Get a count of clients connected
  client_cnt=$(tmux list-clients | wc -l)
  if [ $client_cnt -ge 1 ]; then
    # Make a unique session name
    session_name=$base_session"-"$client_cnt
    # Create the new session based on the base_session
    tmux new-session -d -t $base_session -s $session_name
    # Launch the connection with a few caveats (kill the session when the client goes away)
    tmux -2 attach-session -t $session_name \; set-option destroy-unattached
  else
    tmux -2 attach-session -t $base_session
  fi
fi

Now I can connect many times using PuTTY to a single server and see the same session in all windows (and change one connection to display a different window). I would also like to

Edit

I thought this was working once before, then I just learned that I must have still been in screen, so I have fixed this to do separate sessions. I commented, so let me know if you have any questions. Now there is one downside to this, it will choose the smallest size for both windows. That isn't helpful...I am working on finding a fix currently.

Edit 2

In order to fix the resizing issue, add the following line to your .tmux.conf file

set-option -g aggressive-resize on
Related Question