screen (or gnu screen) and tmux are terminal multiplexers with features supporting command line terminal window management. In this brief introduction we look at maintaining a command line environment session across multiple connection sessions. These programs offer other rich features such as managing display of multiple sessions and customizations which are not covered here.
Why session management? If you are using Linux or Unix in the command line environment, being able to manage the shell environment across various connection sessions can be helpful. You can start a connection from one location, detach and pick up the session from another location. Using one terminal screen you can manage several different sessions. If your connection unexpectedly drops, you can pick up the session after the connection is reestablished.
screen
screen or GNU Screen is the classic full-screen window manager.
If you’ve established a ssh connection to a server with screen installed, enter the command “screen” at the terminal prompt. If your terminal application session disconnects for one reason or another, you can login to the host and attach to the previous session with “screen –r”. This reestablishes your previous environment that the detached screen process maintained for you,
“screen” Create new screen session
“screen –r” Reattach to the last previous session
“screen –list” list existing sessions
tmux
tmux is a more recent offering supporting terminal multiplexing features. There’s a bit more excitement in the tmux community as it’s the new kid on the block and enjoys active development with some strong features. Here’s a quick summary of connection management related command features:
“tmux” or “tmux new” Create a new tmux session
“tmux new –s session_name” Create a new tmux session named session_name
“tmux detach” Detach the currently attached session
“tmux list-sessions
“tmux attach-session” Attach to most recently used unattached session
“tmux attach-session –t session_name” Attach to session by name of session
Control-b (C-b) is the default escape character, I tend to remap it to C-\ since it affects my command line editing. Some useful commands:
c |
create new window |
n |
go to next window |
p |
go to previous window |
w |
choose window |
" |
split window in panes |
arrow keys |
go to different panes |
q |
display panes |
alt + arrow keys |
resize panes |
An example with tmux
After a command shell has been established via login by ssh or similar, You can invoke the tmux command. Let's create a tmux session named "session_name".
tmux new -s session_name
We can do the usual type of shell based work within this session. tmux establishes the default visual environment including a bottom status bar. Let's see check out the list-sessions option of tmux.
tmux list-sessions
session_name: 1 windows (created Tue Apr 4 14:15:43 2017) [80x23] (attached)
If our session was interrupted (say network trouble or your connecting terminal suddenly turned off) you could login again (perhaps from another location or computer) and see you session remains available.
tmux list-sessions
session_name: 1 windows (created Tue Apr 4 14:15:43 2017) [80x23]
Let's attach to that session.
tmux attach-session -t session_name
Now we are back in business, see the tmux status bar, etc. The session state would be as it would had the disconnection not occurred. We could have also entered "tmux attach" as attach command is an alias for attach-session and it will attach the most recently disconnected session by default. The "tmux list-sessions" can be shortened to "tmux ls".
tmux has some internal command facility using the prefix "Ctrl-b". For example "Ctrl-b d" (control key depressed while tapping b, followed by the d key) sends a tmux detach session command to tmux and so then detaches that particular tmux session leaving your terminal session free to do other shell work (and not disturb whatever that disconnected session had going.) "Ctrl-b ?" shows the list of key bindings, i.e, help.