Robert Zaremba Blog

Keep your session open

Have you ever thought how to keep session open on remote host, or want to run multiple processes in a background?

You may hear about nohup, but it is uncomfortable and doesn’t allow you to come back to this process.

tmux and screen

Multiplex virtual consoles are the way to go. Both are well known and widely used in the Linux world.

I prefer tmux. It has more features, is more comfortable and beauty out of the box. According to tmux FAQ:

  • A clearly defined client/server model (windows are their own clients which allows flexibility on how you handle windows. You can attach and detach different windows in different sessions without any issues)
  • Consistent, well-documented command interface. (You can use the same commands interactively as in the .tmux.conf file, more on that later)
  • Easily scriptable
  • Multiple paste buffers
  • Vi & Emacs keybindings
  • A more usable status line syntax (which also allows you to embed the output of a shell command, handy indeed.

How to start

Imagine you are logged in a remote server and want to download big file. Just enter into virtual console and make the job there:

tmux
## the multiplex will run
wget http://sever.com/some_file

To go back to main console type: prefix d (detach) Then you can logout, and later log in and type:

tmux attach

prefix is a key combination used to make some actions. By default `prefix = Ctrl-b` Since in screen prefix=Ctrl-a, it is nice to use the same prefix. Moreover, in my opinion, Ctrl-a is easier to type. To change the prefix, you need to edit tmux configuration file (~/.tmux.conf)

tmux configuration

# ~/.tmux.conf
## uncomment below if you want to use screen style prefix (Ctrl-a)
# set -g prefix c-a
# unbind c-b
# bind c-a send-prefix

# set vi mode. Enter Ctrl+a [ to navigate like in vi. Enter return to get out of the mode
setw -g mode-keys vi


# force a reload of the config file
unbind r
bind r source-file ~/.tmux.conf

# quick pane cycling
unbind ^a
bind ^a select-pane -t :.+

Working with tmux

  • Commands in tmux can be entered from command line: tmux <command> your console emulator might support tab completion with tmux <tab>
  • in tmux session prefix :
  • directly using key shortcut (like previously mentioned prefix-d to detach)

Session management

tmux can manage multiple session.

  • tmux new -s session_name
    creates a new tmux session named session_name
  • tmux attach -t session_name
    attaches to an existing tmux session named session_name
  • tmux switch -t session_name
    switches to an existing session named session_name
  • tmux list-sessions
    lists existing tmux sessions
  • tmux detach (prefix d)
    detach the currently attached session

Windows and panes

In single tmux session you can have multiple windows/tabs. It is very helpful when creating multiple windows to set them names. On tmux you can divide to multiple panes, each one will be occupied by some window

  • tmux new-window [-n window-name] [-t target window] [command] (prefix c)
    a new window and optionally run there a command. The -t option specify where to put new window (as a which window) - can be in form [session_name:]window_num
  • tmux rename-window (prefix ,)
    rename the current window
  • tmux select-window -t :0-9 (prefix 0-9)
    move to the window based on index
  • kill-window (prefix &)
    kill current window
  • prefix n / p / l / w
    move to next / previous / previously selected window / list windows
  • find-window (prefix f)
    find window by name
  • tmux split-window (prefix ”)
    splits the window into two vertical panes
  • tmux split-window -h (prefix %)
    splits the window into two horizontal panes
  • tmux swap-pane -[UDLR] (prefix { or })
    swaps pane with another in the specified direction
  • tmux select-pane -[UDLR]
    selects the next pane in the specified direction
  • tmux select-pane -t :.+ (prefix o or prefix C-a)
    selects the next pane in numerical order
  • tmux display panes (prefix q)
    Show pane numbers (used to switch between panes)
  • move-window [ -d] [ -s src-window] [ -t dst-window]
  • swap-window [ -d] [ -s src-window] [ -t dst-window]
  • break-pane
    make your pane into its own window

Other useful

  • prefix :
    enter command
  • tmux list-keys (prefix ?)
    lists out every bound key and the tmux command it runs
  • tmux list-commands
    lists out every tmux command and its arguments
  • tmux info
    lists out every session, window, pane, its pid, etc.
  • prefix [
    to navigate like in vi. Enter return to get out of the mode

For Screen users