Linux screen commands - The important commands every Linux user should know
The screen is a very good utility on Linux. Especially when we are using Linux servers, it is very important to use Screens in certain use cases. The reason is that we might have some long-running tasks and at that time we cannot expect the connectivity through SSH to stay intact. There is a high possibility that we might have network issues and we might lose connectivity through SSH. In that case, we still want the tasks to be running in the background and we can always connect back when our internet connection is back.
Restoring a Mongo database can be considered one example of this. Sometimes the database restoration can take several hours and we really don't have to keep the connection to the server until then. We can just create a new screen inside Linux and start the restoration. Now that the process is running on a screen, we can close our laptop and always connect back and get attached to the screen again. This is why I strongly recommend using screens for any long-running tasks.
Let me give an overview of all the basic commands that would be required. I found it really hard to keep a track of all the basic commands that are required. The tutorials I found online were also a bit confusing, giving me a list of commands that are not very useful for me. Every time I work with screens, I find it hard to remember all the commands I regularly use. I thought I will write an article and that way I can also remember the commands for myself.
Creating a new screen
screen -S session
This command will create a new screen and name it a session. Name is not a specific thing, we can call it by any name we want. Just an additional thing for us to remember. In some cases we might use several screens and having a proper name would be easy for us to find out what that screen is all about. There is also no harm in creating multiple screens with the same name.
Detaching from the screen
Now there is this concept of attaching and detaching to the screen. When we create a new screen with the above command, we get attached to the screen automatically. So in order to detach from the screen, we can use the below command
CTRL + A and D
We have to do CTRL + A and then type D to detach from the screen. We will be able to see something like the below saying that we are detached from the screen.
List all the active screens
There is this command that helps us view all the active screens and also lets us know if it is attached or detached.
In this case, I have only one screen active and I have called it session, so I'm able to see that. In the previous step, I detached from the screen so it shows that I'm Detached.
Reattaching to screen
The below command helps us reattach the screen. Here is a small tricky part.
The above command helps to reattach to the screen automatically only if we have one active screen available. If we have multiple screens, it will still try to automatically attach but sometimes it will list out all the screens and want us to be specific.
If you look at the above, I first created one more screen, detached from that screen, and tried to view the available screens. After that when I tried to use the above-mentioned command, it said there are more than one session so I have to specify which session I should attach again.
screen -r 1574370
Now the above command with the screen ID will take me to the respective screen.
Deleting a screen
This is the final thing that anyone would like to do. In order to delete a screen, we can first get inside the screen with the
screen -r command and inside the screen, we have to do the following
CTRL + A and type :quit
After we do CTRL + A and start typing, the typing happens at the bottom of the screen like how it happens inside the VIM editor. After we hit enter, we will be off the screen and the screen will no longer be available.
I hope these commands were useful for you. I personally felt when it comes to screens, these are the basic commands we have to remember and we don't need anything else.
If you like what I'm doing on Hive, you can vote me as a witness with the links below.
Posted with STEMGeeks