Linux Screen Basics

Linux

The “Screen” command is a very useful utility that allows you to create a session (or sessions) which you can run programs in, but in such a way that you can detach from the session (screen) if you needed to logoff your terminal or reboot your client machine, but without loosing what you were doing within your Linux session.

I’ll go over the basics and then i’ll give you a working example of when it can be useful. I’m going to assume you’ve already installed screen on your target machine.

You’ll need to logon to your target machine, either locally on the console or via SSH, which is what i’m doing in this case.

Create a Screen Session

First let’s create our first screen session, we’re using “-S” so we can name the session, you can call this whatever you want, if you’ve got many sessions this can be helpful to identify what you are doing in each Screen (session)

screen -S firstsession

Now type Ctrl-a + d and you’ll detach from that session, you’ll know it worked because you’ll see a message saying you’ve detached. Don’t type “exit” or you’ll close the session completely (we do this later on).

Create a Second Screen Session

Create the second session same as before, but give it a different name.

screen -S secondsession

Now type Ctrl-a + d and you’ll detach from that session.

List Your Sessions

Let’s see our two sessions. At this stage we are out of “screen” just logged on at the normal console. But our two sessions are still running, but we’ve detached from them. If anything was running in those sessions/screens they are still running.

screen -ls

And there they are:

ubuntu@development:~$ screen -ls
There are screens on:
        208654.secondsession    (09/03/23 14:02:38)     (Detached)
        208642.firstsession     (09/03/23 13:59:53)     (Detached)
2 Sockets in /run/screen/S-ubuntu.

Swap Between First and Second Screen Session

Let’s connect to the first session again.

screen -r firstsession

And run the command echo “First Session”, to make it obvious which is which.

Now swap to the second session by pressing: Ctrl-a + d, and then:

screen -r secondsession

We’re in the second session now, as you can see we’re not seeing the echo command. Now swap back to the first by pressing: Ctrl-a + d and then:

screen -r firstsession

And now you can see the echo command we did earlier.

Exit Second Screen Session

To exit the session, i.e. not detach it, but close it all together, we first need to reattach the session with:

screen -r secondsession

Then we run “exit”, and you’ll see a “[screen is terminating]” message. If we run “screen -ls” we can then see only the first session is available.

Working Example

Okay how about a workable example. Here’s a common one, you’re about to start a program running, and its likely to take a while, for example a log gather for support, or you’re running a large download or an update and you don’t want it to just stop when you logout; let’s say its just before 5pm and you want to be heading home!

First let’s open the session and call it “updates”:

screen -S updates

Now let’s set a big file to download, of course depending on your bandwidth it might go quickly!

cd /tmp
curl https://releases.ubuntu.com/22.04.3/ubuntu-22.04.3-desktop-amd64.iso

Now hit: Ctrl-a + d to detach the session.

And now “exit” to completely close your SSH session.

And now you can SSH back into your target machine again. Then run the following to reattach to our running session:

screen -r updates

As you can see its still downloading quite happily and it was not interrupted by us detaching from the session nor was it interrupted by us disconnecting our SSH session completely. Just think what would have happened if you disconnected your SSH Session, the curl program would have been stopped and the download ceased, and you’d be starting from square one again.

Manually stop the download with Ctrl-C, and that’s it you’ve done a basic walk-through of using Screen.

Conclusion

So what have we seen? Well, we’ve been over some common usage of the Screen utility and seen how it can help you to maintain a session which is long running when either you need to be able to come and go to the session but not interrupt the running program or allow you to run multiple programs at the same time side-by-side in different screens (sessions).

Image Attribution

1 thought on “Linux Screen Basics

  1. Watch for the existing session, if you got disconnected (not of your own choosing), you may find the session is already attached, which gives the error: “There is no screen to be resumed.” if you try to resume it.

    In this situation you need to detach it from the other one first, with something like: “screen -d -r .” for example: screen -d -r 208642.firstsession

Leave a Reply

Your email address will not be published. Required fields are marked *