How to Kill a Screen Session in Linux?
In Linux, a screen session is a way to run multiple terminal sessions within a single shell window. It allows you to detach from a terminal session and then re-attach it later.
It is important to kill a terminal session, including a screen session, because it frees up system resources and prevents processes from running indefinitely. If a process is left running in the background of a terminal session, it can consume system resources even after you close the terminal window. In some cases, this can cause system instability or even crashes.
Considering the importance of killing a screen session, this post will demonstrate numerous methods to kill a screen session on Linux.
Note: The commands to kill a screen session are the same across all the Linux distributions. As a reference, we will perform the practical demonstration of these commands on Ubuntu 22.04.
Method 1: Using the screen Command
The simplest method for killing a screen session is by using the screen command. To do this, you first need to list all active screen sessions using the following command:
$ screen -ls
This will display a list of all active screen sessions, their process IDs (PIDs), and other information.
To kill a specific screen session, you need to put the session ID by following the below-mentioned command:
$ screen -X -S [session ID] quit
Replace [session ID] with the ID of the screen session you want to kill.
As in our case, the session Id is 18313, so the command would be:
$ screen -X -S 18313 quit
When the command is executed, it will show the interface as below:
The screen session has been terminated.
Note: Whenever you are about to terminate a screen session, you will automatically be navigated to the screen command.
Now, let’s verify whether the session has been terminated or not. Again, use the “screen ls” command as follows:
$ screen -ls
The command’s output shows there is no screen session currently onboard.
Method 2: Using the kill command
Another method for killing a screen session is by using the kill command. This method is useful to kill a screen session and all of its child processes.
Step 1: Get the Process ID of the Screen Session
To do this, first, you need to find the PID of the screen session using the following command:
$ ps -ef | grep SCREEN
This will display a list of all processes with “SCREEN” in the name. Look for the process with the name of your screen session, and note its PID.
Step 2: Kill the Screen Session Using its Process ID
Once you have the PID, you can use the kill command to terminate the session and all its child processes. The command provided below will kill the screen session having process id 18460:
$ kill 18460
The specific screen session(s) has been terminated.
For verification, you can use the “screen -ls” command to ensure the screen session has been terminated.
Method 3: Using the pkill Command
The pkill command is a more powerful version of the kill command that allows you to kill processes by name. This method is useful if you need to know the PID of the screen session. However, it is necessary to keep the session’s name in mind.
The pkill command can be used with the process name to kill all the instances. For instance, the command below will kill all the sessions with a screen in their name.
$ pkill screen
This will kill all processes with “screen” in their name, including all screen sessions.
For more satisfaction, use the “screen -ls” command to ensure that the session has been terminated.
A screen session is a powerful feature in Linux that allows running multiple terminal sessions within a single shell window. However, knowing how to kill a screen session is essential to free up system resources and prevent processes from running indefinitely.
This post demonstrated three different methods to kill a screen session on Linux: using the screen command, the kill command, and the pkill command. By knowing these methods, you can efficiently manage and control your terminal sessions and ensure the optimal performance of your system.
Keep visiting Linux-Genie for more tips and tricks.