We’re going to pause the walkthrough of the Ubuntu server for a moment and talk about a program I absolutely adore, screen
If you haven’t heard of it, no worries! If you have, chances are you probably like it as much as I do. This program is essentially a session manager for your terminal, and though that may not sound like much, I’d be willing to bet after reading the below you will start using it.
If you don’t have it on your computer, or just want to ensure you do, you can download and install it after you update your repository lists:
sudo apt-get update sudo apt-get install screen
If you want to start reading about screen on your own, you can read the man page, but it’s pretty straightforward once you learn a few things.
To start screen, type (surprise, surprise)
You will be greeted with the following
After you hit enter or space, you will be greeted with an ordinary looking terminal prompt. that is anything but ordinary. Now that you are running with screen, hit
That’s Control+A, then Shift+/
Which will present you a listing of the screen key bindings
As you can see, there are a lot of features within screen, and I’m only going to cover a few of them below.
First, lets talk about multiple terminals. Say you are editing a config file and need to kill a process. Instead of closing what you are doing and opening htop, you can simply create a new screen by hitting
This will spawn a new terminal for you to do your work in, leaving the one editing the config file untouched. To move between your terminals, you can use the following commands.
To go to the next screen:
To go to the previous screen:
To get a listing of all open screens (arrows to move, enter to bring up that screen):
To exit out of screen completely, you can use either of the following:
Now that we have the basics out of the way, lets get into the good stuff.
Say you are doing something over SSH and your connect drops, you lose all your work, right? Not with screen. The terminal simply disconnects and when you log in, you reattach the screen, with everything intact as you left it. You can simulate this by either force disconnecting (killing) the remote terminal you have open and running, or manually disconnect from your screen session which is useful if you want to, say, close your SSH connection by leave all your scrips/config files/etc. running/open.
To disconnect from the session, you use the following command:
This takes you back to your base terminal, leaving screen running disconnected in the background. To see the screen sessions open and connected or disconnected on your system, use the following command.
Which will give you output similar to what is shown below.
See how top line shows that I disconnected from the session, then after entering the command above, it shows that I have one disconnected session? To reconnect to that session, I would issue the following command (your screen session name will be different from mine, and don’t forget about tab completion!)
screen -r 1160.tty1.myubuntuserver
An viola! I’m back where I was before my internet went down, killing my SSH connection.
Another thing I find useful is when running verbose backups, copies, etc. Thousands upon thousands of lines will scroll past, sometimes for hours. I don’t want to have to stare at all that, I’d rather do other work. But I don’t want to keep checking on that screen to see when it is finished running either; that’s a waste of my time. Instead, you can monitor for activity or silence and be prompted when either happens.
To monitor for activity (or turn off monitoring if you have it on), you use:
To monitor for silence (or turn off monitoring if you have it on), you use:
When the alerts trip, they will appear as a small text bar at the bottom as well:
The last feature I will cover here is logging. To log the input and output of a screen, use the following command (enter it again to stop logging)
This will create a log called screenlog.x where x is the number of the screen window open. This log will be placed in the window’s default directory. Note that existing logs with the same number will be appended by subsequent logging.
Hope this helps make running SSH sessions more efficient and manageable! After a few uses, the commands will be second nature and you’ll wonder how you ever managed without screen.