Re-attachable shell for long-running tasks on remote host: tmux

 

Introduction

This is almost too simple for a whole wiki page, but snippets that maybe useful in emergency situations deserve a clazzes.org page by definition ...

Some tasks run too long for one "office chair session". Some other tasks require a lot of shell environment preparation.

In both situations a long-lived shell that can be accessed from different client machines can be very useful.

tmux provides such a shell.

tmux snippets

The following examples assume that the detachable shell shall run on a remote machine.

Obviously, tmux has to be installed:

sudo apt-get install tmux

Creating a detachable shell

ssh user@host
tmux
# start whatever task shall run

Detaching

Press Ctrl-b d, i.e. press and hold Control, press B, unhold Control, press D.

Re-Attaching

ssh user@host
tmux attach

Frequently used hotkeys

Scrolling

To enter scrolling press Ctrl-b [. Now cursor up/down and page up/down allow arbitrary scrolling. To quit scrolling, press q (for quit).

Frequently used options

OptionPurpuse
-L socket-nameUses specified socket-name instead of 'default'. Useful to run and distinct multiple tmux daemons.
-S socket-pathUses specified socket-path instead of default path and name. Useful to run and distinct multiple tmux daemons.

Further features and reading

tmux can do much more, i.e. run multiple commands (i.e. shells) and so on.

Just read the tmux(1) manpage, especially chapters KEY BINDINGS, COMMANDS, and below.

Â