Linux `screen` Command – Run in Background

The Linux screen command is extremely useful for the cases when you need to start a long-running process on a remote machine.

Even if your connection drops and the SSH session is terminated, with the screen command you can be sure that the process will keep running in the background and the “lost” terminal session can be resumed.

This note shows how install and how to use the Linux screen command to run a process in the background.

Cool Tip: How to Scroll Up while running the screen command! Read more →

Linux `screen` Command

Install the screen using one of the following commands, depending on your Linux distribution:

$ sudo apt-get install screen
- or -
$ sudo yum install screen

Start a screen session:

$ screen

Press the space or return button to close the license agreement and continue to the screen shell, that looks just like a regular terminal window.

Am I Inside a “Screen”? An interface inside a screen session is exactly as the command prompt. To determine whether you are inside the screen or not, execute the echo $STY command. It returns the name of the screen you’re in. If it is null – you are inside the “real” terminal.

To start a named screen session, run:

$ screen -S <session_name>

To execute a command in the background but don’t attach to the screen session (useful for system startup scripts), run:

$ screen -dm <command>
- or -
$ screen -S <session_name> -dm <command>

Detach from the screen (disconnect the screen from the terminal and put it into the background): Ctrl + A then D.

To terminate the current screen session (not put it into the background but close), press Ctrl + D or type:

$ exit

List the currently running screen sessions:

$ screen -ls

Reattach to the screen (resume the detached screen session):

$ screen -r

To reattached to the the specific screen session, run:

$ screen -r <session_name>

Kill the specific screen session:

$ screen -X -S <session_name> quit

To kill all the screen sessions, run:

$ pkill screen

Cool Tip: How to rename a screen session! Read more →

Was it useful? Share this post with the world!

Leave a Reply