Ever wanted to run a script with the terminal closed? Or do you want to maximize the usage of a terminal? In this tutorial, you can achieve both results with the help of tmux. This tutorial together with the tmux cheat sheet at the end of this tutorial will help you achieve the both scenarios described above!
To install tmux type the following:
sudo apt-get install tmux
How Tmux Works
First, we will understand how tmux works before moving on to the usage.
When you execute a tmux command, a tmux server is launched.
Every activity that is launched in the tmux window happen within the server.
Commands or scripts ran within this server continues even after the SSH daemon has closed.
There are three terms while using tmux.
A session is a single collection of pseudo terminals under the management of tmux.
Each session has one or more windows linked to it.
A window occupies the entire screen and may be split into rectangular panes, in which each pane is a separate pseudo terminal.
First I will ssh into a virtual private server(VPS) hosted by RamNode(aff link).
To create a new Session, issue the following command:
tmux new -s session1
To detach a session, type the following:
(Ctrl + b) + d
Note: Type “Ctrl+b” follow by the ”d” key separately
Now I will create another session call session2:
tmux new -s session2
And detach the session (Ctrl + b) + d to return to the main terminal.
You can list all the session by issuing the command:
To enter a session that was previously created, type:
tmux attach -t
In this tutorial, we will be attaching to session1. Therefore, the command will be:
tmux attach -t session1
Now we will move on to the concept of Window.
Type (Ctrl + b) + c to create a new window.
To switch between the windows, enter the following:
(Ctrl + b) + n (Next window)
(Ctrl + b) + p (Previous window)
Note: The asterisk* is an indicator to the current window
Now I will switch to the first window using (Ctrl + b) + p and rename it using (Ctrl + b) + ,
If you have a lot of windows, you could type (Ctrl + b) + w to list all windows and select using the Enter key.
To delete the current window, type:
(Ctrl + b) + &
Now let’s move on the concept of Panes.
Panes are windows that are divided into multiple parts.
In a window, you can split the current into half horizontally or vertically by issuing:
(Ctrl + b) + “ (horizontally)
(Ctrl + b) + % (vertically)
I will now split the window horizontally using (Ctrl + b) + “ (horizontally).
Now I want to split the top half vertically.
However, the focus is on the 2nd pane.
Change the pane by typing (Ctrl + b) + o (Rotate clockwise) to change the selected pane.
Next split the first pane further vertically by issuing (Ctrl + b) + %
Now you may ask, how is this useful?
A sysadmin could monitor different things at the same time without switching terminal all the time.
And since this can be done over ssh, it means the sysadmin could access these live data anytime.
Tmux Cheat Sheet
|Create new session||tmux new -s sessionname|
|Detach current session||(Ctrl + b) + d|
|List all sessions||tmux ls|
|Create new window||(Ctrl + b) + c|
|Switch windows||(Ctrl + b) + n (Next window)|
|(Ctrl + b) + p (Previous window)|
|List all windows||(Ctrl + b) + w|
|Delete current window||(Ctrl + b) + &|
|Split panes||(Ctrl + b) + “ (horizontally)|
|(Ctrl + b) + % (vertically)|
|Switch panes||(Ctrl + b) + o|