Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Kill a Detached screen Session on Linux
Screen sessions are an excellent way to run background processes on a Linux machine. However, sometimes it becomes necessary to terminate a detached screen session that is no longer needed. This article demonstrates how to kill detached screen sessions on Linux using various command-line methods.
When using the GNU screen tool, we can sometimes accumulate detached sessions that require cleanup. We'll explore several approaches for terminating these disconnected screen sessions safely and efficiently.
Listing Active Sessions
Before terminating sessions, let's first examine how to view existing sessions. First, we'll create a couple of sample screen sessions. In a bash shell, execute the following commands
$ screen -dmS my_session_1 $ screen -dmS my_session_2
This creates two sessions named my_session_1 and my_session_2. Note that we are not attached to either session (thanks to the -d flag). Now, let's view the sessions we created
$ screen -list
The output displays our two detached sessions
There are screens on:
84581.my_session_1 (Detached)
76340.my_session_2 (Detached)
Method 1: Attach and Kill
One approach to terminate a screen session is to attach to it first, then exit. Let's attach to the first session
$ screen -r my_session_1
Once attached, we can terminate the session by typing
$ exit
The session will terminate and display
[screen is terminating]
Now only one session remains
$ screen -list
There is a screen on:
76340.my_session_2 (Detached)
Alternative: Using Quit Command
If the screen session contains multiple windows, you would need to type exit in each window. A simpler alternative is the quit command
CTRL+a \
This prompts for confirmation
Really quit and kill all your windows [y/n]
Typing y will terminate all windows in the session immediately.
Force Detach and Attach
If another terminal or user is attached to a session you want to terminate, use the force detach option
$ screen -D -R my_session_name
This detaches any existing connections and attaches the session to your current terminal.
Method 2: Kill Without Attaching
A more efficient approach is terminating screen sessions without attaching to them first. Let's create test sessions
$ screen -dmS my_session_4 $ screen -dmS my_session_5
Verify the sessions exist
$ screen -list
There are screens on:
19665.my_session_4 (Detached)
19671.my_session_5 (Detached)
Using the -X Flag
The -X flag allows sending commands to a specific screen session. To send a quit command to my_session_4
$ screen -S my_session_4 -X quit
Verify the session was terminated
$ screen -list
There is a screen on:
19671.my_session_5 (Detached)
Method 3: Using Kill Command
Screen sessions can also be terminated using operating system commands. The numbers preceding the session name represent the PID (Process ID) and can be used with the kill command
$ kill -9 19671
This immediately terminates session 19671 and all processes running within it.
Comparison of Methods
| Method | Command | Advantages | Use Case |
|---|---|---|---|
| Attach & Exit | screen -r name; exit |
Graceful shutdown | Single window sessions |
| Quit Command | CTRL+a \ |
Handles multiple windows | Complex sessions |
| Remote Quit | screen -S name -X quit |
No attachment needed | Scripting/automation |
| Kill PID | kill -9 PID |
Force termination | Unresponsive sessions |
Conclusion
This article covered multiple methods for terminating detached screen sessions on Linux. The screen -X quit command is most efficient for routine cleanup, while kill -9 provides a forceful option for unresponsive sessions. Remember that terminating a screen session will also kill all processes running within it, so exercise caution when using these commands.
