How to create and manage multiple console sessions via ssh?
Screen is a window manager for the console. It is possible to create and manage several virtual console sessions via a single access (e.g. ssh). In addition, sessions can be disconnected and resumed later.
Here is a small example to demonstrate the power of screen. You log in to a server via ssh and start a program or pull a large Docker image. When you end the ssh session, the program stops automatically.
With screen you can prevent this behavior. You log in via ssh. Then start screen and the desired program. In the next step, you call the detach command and can log out. The program continues to work in the background. You can resume the session with the program the next time you log in via ssh.
In this post, we will introduce the basic functionalities of screen. The steps are the following:
- Install screen
- Difference between session and window
- Important commands
- Conclusion
Install screen
Use the following command to install screen:
sudo apt-get install screen
You can start screen with the following command:
screen
After calling screen, it seems as if nothing has happened. But this is not true, because screen is now running in the background.
Difference between session and window
You can imagine screen like a web browser. A screen session corresponds to a browser window and a screen window corresponds to a browser tab. Several screen sessions can run at the same time. In addition, several screen windows can be opened in each screen session.
Important commands
First, you create a new session. The following command creates a new session with the name “session1”.
screen -S session1
Keyboard shortcuts
- Ctrl + A + C: Creates a new window in a session
- Ctrl + A + Space: Switching between the individual windows of a session
- Ctrl + A + D: Disconnect (detach) the connection to the current session. The session then continues to run in the background
More commands
Resumes the session with the name “session1”:
screen -r session1
Resumes the session if only one session exists:
screen -r
Listing of all ongoing screen sessions:
screen -ls
Conclusion
The window manager screen allows tasks to perform in the background. For example, you can connect to a remote computer via ssh and start a program. With screen, you can start the program and close the ssh connection again.
💡 Do you enjoy our content and want to read super-detailed articles about data science topics? If so, be sure to check out our premium offer!
Leave a comment