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 screenIf using AlmaLinux, Rocky Linux & CentOS, then you can use this command
sudo dnf install screen2. Create a New Session with Screen
When screen is installed, you can create a new session with the following command
screen -S wpgan-Sis used to give the session a name.wpganis the name you set and it can be changed as needed.
After the command is executed, you will enter the Screen session.
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/screen3. 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 dateThis 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.
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 + ADAfter that, you will return to the regular terminal, while the command watch will keep running in the background.
5. Check the Screen Session List
To see all active sessions, use the following command:
screen -lsYou will then be able to see the list of screens that have been created and are running
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 wpganTo use the ID, you can use
screen -r 1715If 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.
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 wpganThis command will detach the session from the previous terminal and then immediately open it in the terminal you are currently using.
8. Closing a Screen Session
To close a session, you need to first enter the session that will be closed
screen -r wpganBecause the example uses the command watch, stop the process first by pressing CTRL + C, then you can exit by running the command exit.
Commonly Used Screen Commands
The following is a list of Screen commands that are commonly used
screen -S namaCreate a new sessionscreen -lsView the session listscreen -r namaReopen a sessionscreen -d -r namaForce reattach sessionexitClose 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
wgetorcurl. - 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.
COMMENTS
Loading...