Most Linux administration tasks are carried out over SSH. We don’t usually have direct, physical access to the server, and so remote SSH connections are the norm. Over the CLI, we can perform just about any administration tasks that need to be done, with the possible exception of some low level hardware configurations.
But an Internet connection can be an unstable thing at certain points of time. Even the best and most reliable ISPs experience occasional glitches. And while it may not happen often, even once can be enough to completely mess up a complicated administration task – one with multiple tasks running in the background, configuration files open for editing etc etc.
In a previous article, I’d talked about an awesome program called “screen”, which allows you to have multiple “windows” open in a single SSH session, thereby allowing admins to focus on more than one task at a time if necessary. However, its utility doesn’t end there. Among the benefits of screen is the ability to resume your work in case an SSH session gets accidentally disconnected.
How to Resume a Disconnected Session with Screen
In the previous article, I showed you how to install screen and start a session (perhaps with multiple windows). The good news is that if you accidentally disconnect from your SSH session, your screen window will still be there waiting for you when you come back – even if you SSH in from a different machine altogether!
This ability of screen to preserve itself even in the absence of an SSH session is called “detaching”. Screen automatically detaches itself when a session ends unexpectedly. In fact, you can even detach it manually using the command:
Let’s look at an example. Here is a screen window with a text editor running:
Now I just close my SSH session without logging off. To the server, it appears that I’ve just broken the connection – it could well be because my Internet connection is gone. The next time I log in, I can see the detached screen sessions using the command:
Here’s what happens when I run “screen -ls” after logging in again:
As you can see, it says that as screen is available. The screen is referred to by the name that follows – a number, a dot, and a hostname. To re-connect or re-attach to this screen, just type:
And you’re back in business! If you want, you can have multiple screen sessions by just running screen again. Here’s an example of the output of “screen -ls” when there are multiple screen sessions.:
You can connect to a specific screen session by typing:
screen -r [session name]
Renaming your Screen Sessions
As you can see above, the names are not very descriptive. It’s difficult to tell which screen session is doing what. So if you want to go to the screen that’s performing a certain task, you’ll just have to cycle through them all. To rename a screen session, open it and type:
This will open up a text field at the bottom of the screen where you can enter commands. Type the following:
sessionname [new session name]
Where [new session name] is the name you want to give to your particular screen session. In the screenshot below, I’m just echoing a command, so I name it accordingly:
Now when I detach and type “screen -ls”, I get the following:
As you can see, the session name has been changed and I now know which session is doing what!
Using a Password to Protect your Screen Sessions
Another nifty feature of screen is the ability to step away from your workstation and have your sessions protected by a password. To active this, just type:
Now your screen will be locked and anyone who wants to continue working needs to type in your password to continue:
With all these features, I consider screen to be one of the most essential tools available for Linux administration!
Is your website slow?
Enter its URL below to find out now:
- PREVIOUS ENTRY: How to Have Different Windows in Linux with a CLI
- NEXT ENTRY: How to Hide the Header on Specific Pages in WordPress