Launch a command via SSH, and let it run after disconnecting.

Launch a command via SSH, and let it run after disconnecting.

If you're used to connecting to servers via SSH, you know what it's like to have to stay connected to prevent a command or process from stopping. There's a handy solution that lets you launch a command and either resume it later or let it run in parallel with the `screen` command.

System preparation

First, install the screen package via the package manager.

sudo apt install screen
In principle, the package is already installed on some distributions, notably Ubuntu

Launch a command in a detached screen

The principle of the operation is to create a virtual screen where the command will be executed. This means that its execution will not depend on the SSH connection from which the command is launched.

To do this, run the following command:

screen -dmS SCREENAME COMMANDE
Replace SCREENNAME with a desired name (allows differentiation between different screens), and the desired command in place of COMMAND.
No need to put it in quotation marks.

Once the command is executed, it immediately launches in a detached screen and is already running. Connecting to a detached screen involves two steps:

First, display the list of screens currently rotating

screen -ls

This command displays a list like the one below, for example:

There are screens on:
        435253.check_integrity  (12/04/2025 04:15:20 PM)        (Detached)
        380843.delete_doublon   (12/04/2025 04:08:43 PM)        (Detached)
2 Sockets in /run/screen/S-localadmin.

The numbers before the dot are the process PID, and after the dot is the name of the screen given previously.

In our example, we want to connect to the first screen, which is called check_integrity. The command to type is therefore:

screen -r 435253.check_integrity
The command `screen -r` means "attach", and will therefore connect to the virtual screen to display the content.

Once we're finished with the screen, and we need to detach from it again, simply press the keyboard shortcut CTRL + A, and then D.

First press CTRL + A, and after releasing both keys, press the A key.

And there you have it, our screen is detached again and running in the background. It's now possible to disconnect from the server without interrupting the screen execution.