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.
screen -ls
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.
screen -r
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
I mostly use windows however I have a Raspberry Pi II in here. I bet this will come in handy when I decided to tinker on my Pi again in the near future. 🙂
Thanks for sharing!
!1UP
I'm sure it will. Good luck with that. Cheers!
Those are useful things to remember! You cant beat the security you get with Linux, but man using the command line prompt is sometimes a struggle! 😅
This post has been manually curated by @bhattg from Indiaunited community. Join us on our Discord Server.
Do you know that you can earn a passive income by delegating to @indiaunited. We share 100 % of the curation rewards with the delegators.
Here are some handy links for delegations: 100HP, 250HP, 500HP, 1000HP.
Read our latest announcement post to get more information.
Please contribute to the community by upvoting this comment and posts made by @indiaunited.
You have received a 1UP from @thecuriousfool!
@stem-curator, @vyb-curator, @pob-curator, @neoxag-curator, @pal-curator
And they will bring !PIZZA 🍕.
Learn more about our delegation service to earn daily rewards. Join the Cartel on Discord.
PIZZA Holders sent $PIZZA tips in this post's comments:
curation-cartel tipped balaz (x1)
@gwajnberg(2/15) tipped @balaz (x1)
Please vote for pizza.witness!
Congratulations @balaz! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):
Your next target is to reach 2500 upvotes.
You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
Check out the last post from @hivebuzz:
Support the HiveBuzz project. Vote for our proposal!
you are right! screen function is very useful with servers...before knowing about that I was using only nohup which runs programs in the background, but you can see only the log....
!PIZZA
Check out tmux, https://github.com/tmux/tmux/wiki.