Keep your Ubuntu Server tasks running with Screen

Sometimes it can be useful to leave your server working on a task in case you need to disconnect yourself from it. With screen we can create and resume sessions easily.

To install screen on Ubuntu, type the following command:

$ sudo apt update
$ sudo apt install screen

Is always a good idea to update the local package list from your server by running apt update before installing a new package. That way, you know you’re installing the latest software available and avoiding compatibility issues. Once installed, you can check it with:

$ screen --version

Now we can use man to check the documentation. In the description, we can see that once we create a screen: “Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the user’s terminal.” So, let’s get into it. To create a screen we simple type:

$ screen

We will see the some info about screen and after pressing Enter or Space we will see a new line of the terminal. We have successfully create a new screen. You can run a command or perform a task and detach from that screen by simply pressing Ctrl + A and then D.

To see the amount of screens created and their details, we can use:

$ screen -ls

That command will output something like this:

$ screen -ls
There are screens on:
        1729.pts-4.serverName-001  (01/22/22 22:47:21)     (Detached)
        1633.pts-4.serverName-001  (01/22/22 22:39:26)     (Detached)
        1402.pts-1.serverName-001  (01/22/22 22:31:18)     (Detached)
3 Sockets in /run/screen/S-user.

So, if you want to resume one screen, you can reference it with the whole name using first screen -r:

screen -r 1633.pts-4.serverName-001

This is simple and effective way to run task in your server even after you get disconnected from it. Pretty useful!

| Theme: UPortfolio