Hey techies! How’s it going? I couldn’t share a blog post on unix for a while. So I thought of writing one for you guys and you are going to love it(wink-face). Do you know what we are going to talk about? Today we are going to talk about “Screen“(excited-face). The screen command works on both unix based operating systems such as linux, mac. Brace yourself mac/linux lovers. It’s a very useful command. I’m sure once you learn it, you are gonna use it everyday. We’ll start with the basics. Then we’ll dive into some command line examples as well. Are you ready? here we go(happy-face).

What’s the Screen Command?

Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells). Each virtual terminal provides the functions of a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets).

That’s what man page says but I would like to describe it in plain english so that everyone can understand what it is. It’s like this. The screen program allows you to use multiple windows in Unix. In other words the screen command lets you detach from a terminal session and then attach it back at a later time. To be able to do that you should be in a screen session. Just using the terminal won’t let you do that. Best part is even when you are detached from the terminal session, the process that was originally started under the screen session is still running and managed by the screen. You can reattach to the particular screen session at a later time when you need to and your terminals are still there, the way you left them. You got nothing to worry about(happy-face).

How to install the screen command(only not exists)?

The Screen command has been installed by default in most linux system as well as mac os x. But, in some Linux systems do not have screen installed by default, So you need to install it first using apt-get command before using it. Check out the below command. You might need sudo for this.

# apt-get install screen

When to use the screen command?

This is a very interesting part before we jump into how to use it. Knowing the commands won’t help you if you don’t apply them in your work to get the work done effectively and efficiently. That’s why I’m here to teach you. You might find the screen command helpful in many cases. Here is my list. By the way if you are already using the screen command and if your way of using the screen command is not in the list below, don’t forget to tell me in the comments below.

  • When you only can access single terminal, you can use the screen command to make the single terminal into multiple, and execute several commands in different screen sessions.
  • When you want to make a process keep running but not as a daemon(so whenever you want you can check what’s happening by attaching to the particular screen session) while you are make use of the terminal for other work.
  • When you want to have a command line session password protected. In other words locked.
  • When you want to have a command line session shared among few others user. It’s called multi user sessions.

How to use the screen command?

Okay it’s that moment you have been waiting for. How to use?(excited-face) This is what we are going to do. I’ll teach you the basics. If you are interested you can find out about the screen command more. Let’s get our hands dirty. I mean start using the screen command(laugh-face). Note that you are on a mac you will have to use control key instead of ctrl key.

Create a new Screen session
screen #creates a default session
screen -S name #creates a session with a name. name can be used to reattach at a later stage

If you don’t provide a name it uses [pid.]tty[.host] format to name the screen session. pid will be used when reattaching to the screen session.

Detach from a screen session
Ctrl-a d #detach from the current screen session you are in
screen -d SCREENID #when the command screen is running in another terminal, remote detach
List screen sessions
screen -ls #it will list all the existing screen sessions
Reattach to a screen session
screen -r #attaches to default session. this works only if you have one screen session
screen -r <SCREENID> #attaches to particular screen session
screen -r name #attaches to specific session (unique name specified during the creation of the screen session )
Kill the attached screen session
 CTRL-a k #it will ask "Really kill his window [y/n]". y for yes, n for no
Kill the deattached screen session
screen -X -S SCREENID quit
screen -S SCREENID -X quit
Screen session with logs
screen -L #-L Tell screen to turn on automatic output logging for the windows.

The output is saved to screenlog.n file, where n is a number. To view the file use below commands. You can also use Ctrl-a + H to save logs into screenlog.n file. One more Ctrl-a + H to turn off. Is it possible to name the ‘screen’ logfile from the -L flag? Yes.

less -r /home/dasunhegoda/screenlog.0
cat /home/dasunhegoda/screenlog.0
Lock screen session
CTRL-a x #it will lock your screen session quickly while you are in the screen session

Tips & Tricks

How to scroll up in a screen

When you are in a screen you can’t scroll up like in the command line. To be able to scroll you have to follow below steps.

Step 1

CTRL-a Esc

Step 2

Press the “Up” and “Down” arrow keys or the “Page Up” and “Page Down” keys to scroll through the output.

Step 3
Press “Esc” to exit scroll back mode(copy mode).

Screen key bindings

Below will show all the screen key bindings. You don’t have to remember a thing.

CTRL-a ?

You might want to go through the man page as well. Check out this link and this link to get to know more about screen command. That’s all about the screen command. Do you have any question about the screen command? Comments below are open for you. Your feedback is highly appreciated (happy-face).

Screen Command Start Page
Screen Command Start Page

 

Loading