How to get best out of your SSH with GNU screen/Tmux?
UPDATE: GNU screen sometimes create its own environment and hence some commands might not be accessible. On the Contrary tmux uses the same environment.
Using external hardware with SSH can be painfully hard if you don't know how to use it.
Major issues
- If you want to execute a process that will take more than 10 hours to run, you need not keep your Machine connected with SSH for 10 hours. If you just run the task while you are on SSH and log off. Log in again after 10 hours. You will find the process was forcefully terminated by system after few minutes you logged off.
- You want to open multiple terminals in ssh. So should you just ssh from different terminals in local machine?
then GNU screen got you covered.
GNU screen
GNU screen is a full screen window manager that multiplexes a physical terminal between several processes. You can have multiple terminals from single terminal with each working as a separate process and that will not terminate when you log off.
To get started, install screen with apt install screen
(apt install tmux
for Tmux)
To start a screen enter screen
in bash terminal. This will create a new screen and display that window.
All commands in screen start with ctr + a that is
press and hold ctr then press ‘a’ then release both keys then type the command.
Lets list out basic commands first and then see exact use case
- ctr + a c : create new screen
- ctr + a d : detach from the screen. The screen will keep on running in background as a process
- ctr + a n : navigate to next screen in screen list
- ctr + a p : navigate to previous screen in screen list
- ctr + a | : split window vertically into two windows and create space for new screen
- ctr + a S : split window horizontally into two windows and create space for new screen
- ctr + a tab : navigate between windows
exit
: to kill the current screen and land back to original terminalscreen -S name
: start screen with given namescreen -r
: resume screen
For Tmux just replace ctr + a with ctr +b and all other commands remain the same.
Use cases:
1. To keep processes running after ending SSH session (training a large model, HPC calculations):
Create a new screen with screen
and then run the process that you want. Example, python3 train_model.py
Then detach your screen with ctr + a d. This will detach your screen and leave it in background to run
log off the SSH and come after predicted run time, say 10 hours. You can resume the screen session with screen -r
. If you have multiple sessions list screens with screen -ls
Then resume particular session with session id screen -r 1044
.
There is a quicker way to do this. Just type screen -d -r python3 train_model.py
. This will create a new screen in background and start your process and will kill the screen session once execution completes.
2. Work with multiple terminals in a single SSH terminal:
Lets say you want to work with multiple terminals but want to avoid multiple ssh logins or maybe its not allowed at all. You can just create multiple screens in current SSH session and use split screen.
For example,
- create new screen: ctr + a c. Lets say start
htop
to view your current resources usage. - Again create new screen with ctr + a c and start new process.
- Now use ctr + a + | to split screen vertically. Use ctr + a tab to navigate to new window. Use ctr + a n to bring next screen in current window
- Similarly use ctr + a S for split screen horizontally.
Now you have something looking like this.
Multiple terminals with a single SSH!
Follow me for more such info: