Running Programs In The Background With Screen

5 October, 2020

Installation

Change apt to the package manager you want to use.

$ apt install screen

Usage

Run script in a ‘screen’

$ screen ./scriptName.sh

List screens

$ screen -ls

Resume a screen session

One session running

$ screen -r

Multiple sessions running

If you have multiple screen sessions running, you have to list them first to get their ids:

$ screen -ls
There are screens on:
        784.pts-1.108124a2ab91  (10/05/20 19:18:54)     (Detached)
        622.pts-1.108124a2ab91  (10/05/20 18:56:33)     (Detached)
        1024.secretStuff        (01/05/53 00:00:00)     (Detached)

Then you can resume a session by using its ID (the first number before the . or name of the session):

$ screen -r 784

In my case, I could have replaced 784 in the command above with 622, 1024 or secretStuff.

Exit a screen

To exit a screen and let it run in the background you can, while holding control press A + D.

Rename a screen

Replace mySessionsID with the id or name of the session you want to rename and newName with the desired name for the session.

$ screen -X -S mySessionsID sessionname newName

Delete a screen

Replace mySessionsID with the id or name of the session you want to stop.

$ screen -X -S mySessionsID quit