How to Use Screen on Linux

How to Use Screen on Linux

In this tutorial, we will discuss how to use Screen on Linux and some common commands used when working with Screen.

Screen is a terminal multiplexer that allows processes to keep running in the background even after you log out of the server. This way, you can return to that session at any time without needing to restart your applications.

When running applications via SSH, the running process usually stops when the SSH connection is lost or the terminal is closed. To overcome this, you can use Screen.

How to Use Screen on Linux

Before proceeding to the steps for using Screen on Linux, if you are accessing via SSH, ensure you can already access it via SSH, or you can follow the tutorial How to Log In to a Linux VPS via SSH first.

If you can already access SSH, you can start from the first step.

1. Install Screen

Depending on the OS you are using, the installation steps for Screen will be slightly different. For Ubuntu, you can use this command

sudo apt update && sudo apt install screen

If using AlmaLinux, Rocky Linux & CentOS, then you can use this command

sudo dnf install screen
Cara Menggunakan Screen di Linux - WPGan.com - 1

2. Create a New Session with Screen

When screen is installed, you can create a new session with the following command

screen -S wpgan
  • -S is used to give the session a name.
  • wpgan is the name you set and it can be changed as needed.

After the command is executed, you will enter the Screen session.

Cara Menggunakan Screen di Linux - WPGan.com - 2

If you get an error message Directory '/run/screen' must have mode 777.when creating a session for the first time, you can fix it with

sudo chmod 1777 /run/screen

3. Run Applications inside Screen

Once inside the Screen session, you can run applications as usual. For example, to test it, you can use

watch -n 1 date

This command will display the date and time, which updates every second.

This example was chosen because it is easy to use to prove that the process keeps running even if you exit the session.

Cara Menggunakan Screen di Linux - WPGan.com - 3

4. Exit a Session Without Stopping the Process

To exit a session without stopping the application, you can press the following keys in sequence.

Ctrl + AD

After that, you will return to the regular terminal, while the command watch will keep running in the background.

Cara Menggunakan Screen di Linux - WPGan.com - 4

5. Check the Screen Session List

To see all active sessions, use the following command:

screen -ls

You will then be able to see the list of screens that have been created and are running

Cara Menggunakan Screen di Linux - WPGan.com - 5

6. Re-enter a Screen Session

To reopen a session that was previously created, you can use ID session or you can also use the screen name.

screen -r wpgan

To use the ID, you can use

screen -r 1715

If successful, you will return to the view watch which is still updating the time. This indicates that the process did not stop even though you had previously exited the session.

Cara Menggunakan Screen di Linux - WPGan.com - 6

7. Force Reattach Session

If a message appears stating that the session is being used in another terminal, you can add the parameter -d when entering the session

screen -d -r wpgan

This command will detach the session from the previous terminal and then immediately open it in the terminal you are currently using.

Cara Menggunakan Screen di Linux - WPGan.com - 7

8. Closing a Screen Session

To close a session, you need to first enter the session that will be closed

screen -r wpgan

Because the example uses the command watch, stop the process first by pressing CTRL + C, then you can exit by running the command exit.

Cara Menggunakan Screen di Linux - WPGan.com - 8

Commonly Used Screen Commands

The following is a list of Screen commands that are commonly used

  • screen -S nama Create a new session
  • screen -ls View the session list
  • screen -r nama Reopen a session
  • screen -d -r nama Force reattach session
  • exit Close a session

When Should You Use Screen?

Screen is suitable for use when you are running processes that take a long time or want them to keep running even if the SSH connection is lost, for example:

  • Running Node.js or Python applications.
  • Running a server Ollama.
  • Downloading large files using wget or curl.
  • Backing up or synchronizing files using rsync.
  • Encoding video using FFmpeg.
  • Downloading videos using yt-dlp.

FAQ: How to Use Screen on Linux

What is the difference between Screen and TMUX?

Both have almost the same function, which is to run multiple terminals in one session and keep processes running when the SSH connection is lost.

The difference is that TMUX offers more modern and flexible features, while Screen is simpler and generally already available on many Linux distributions.

Does Screen keep running after an SSH logout?

Yes. As long as the session has not been closed and the server has not been restarted, all processes inside Screen will keep running.

Does Screen automatically restart after a server reboot?

No. All Screen sessions will stop when the server is restarted. If you need applications to run automatically after a reboot, use a service like systemd or a process manager like PM2.

Written by

Muhammad Khoirun Nasikin

I enjoy tinkering with WordPress, hosting, VPS, web development, and various small experiments related to websites. I have over 3 years of experience in the web hosting industry.

COMMENTS

Loading...