Imagine having to execute a long running process and waiting for it to finish, most likely you are connected via ssh and you can’t do anything else but waiting for that process to finish. When the process is taking too long your connection will time out and you will be kicked out. When that happens your process will stop its execution and you will have to rerun it. This is painful and here is where screen comes into the picture.
For example, you are downloading a big file or a database using mysql dump command. This usually takes 10 to 20 minutes.
You can use the Linux screen command to push running terminal applications or processes to the background and pull them forward when you want to see them. It also supports split-screen displays and works over SSH connections, even after you disconnect and reconnect.
How does screen work?
The usual operation with screen is to create a new window with a shell in it, run a command, and then push the window to the background (called “detaching”). When you want to see how your process is doing, you can pull the window to the foreground again (“reattach”) and use it again. This is great for long processes you don’t want to accidentally terminate by closing the terminal window.
Once you’ve got a screen session running, you can create new windows and run other processes in them. You can easily hop between windows to monitor their progress. You can also split your terminal window into vertical or horizontal regions, and display your various screen windows in one window.
You can connect to a remote machine, start a screen session, and launch a process. You can disconnect from the remote host, reconnect, and your process will still be running.
You can share a screen session between two different SSH connections so two people can see the same thing, in real-time.
Intall screen
# ubuntu
sudo apt update
sudo apt install screen
# centos or fedora
sudo yum install screenStart a screen session
screen
# start screen session with a name
screen -S loading-databaseThis will open a screen session, create a new window, and start a shell in that window.
Most used commands for a screen window
Ctrl+acCreate a new window (with shell).Ctrl+a"List all windows.Ctrl+a0Switch to window 0 (by number).Ctrl+aARename the current window.Ctrl+aSSplit current region horizontally into two regions.Ctrl+a|Split current region vertically into two regions.Ctrl+atabSwitch the input focus to the next region.Ctrl+aCtrl+aToggle between the current and previous windowsCtrl+aQClose all regions but the current one.Ctrl+aXClose the current region.
Detach from a screen session
Ctrl+a Ctrl+d
The program running in the screen session will continue to run after you detach from the session.
Reattach to a screen session
# resume current screen session, assuming one screen session
screen -rIn case you have multiple screen sessions running on your machine, you will need to append the screen session ID after the r switch.
# list the current running screen sessions
screen -ls
# output
There is a screen on:
30093.pts-0.ip-172-31-8-213 (09/24/21 16:07:23) (Attached)
1 Socket in /run/screen/S-ubuntu.
# command
screen -r 30093Terminate a screen session
Make sure you on the screen session and then run the exit command.
exitor run this command out of the screen session
screen -X -S screen_id kill (or quit)
or
screen -XS <screen_id> kill (or quit)
# Example
screen -X -S 30093 kill
or
screen -XS 30093 quit