Kill detached screen session
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
GNU Screen is a terminal multiplexer that allows users to manage console-based applications in a single terminal session. One of the powerful features of Screen is the ability to detach from a session and leave running applications in the background. However, managing these sessions, especially terminating them, can be crucial for system administration, freeing resources, or terminating unneeded tasks.
Understanding Screen Sessions
When you start a new Screen session, it creates a virtual terminal that continues running on the server even after you disconnect from it. This functionality is great for running long processes on a remote machine without maintaining an active connection. However, these sessions may need to be killed or terminated for various reasons such as maintenance, resource allocation, or when a session hangs or becomes unresponsive.
Listing Screen Sessions
Before you can kill a detached screen session, you must first identify it. You can list all the current Screen sessions with:
This command outputs a list of current screens, showing whether they are attached or detached, and provides a unique identifier for each session, typically in the format pid.tty.host.
Killing a Detached Screen Session
To kill a detached Screen session, you need its session identifier or PID. Once you have identified the session you want to kill, you can use the screen -X -S [session #] quit command:
Here, 12345 should be replaced with the actual session number from the screen -ls command output.
Alternatively, if you want to kill the screen session using the PID, you can use:
Where 5678 is the PID of the detached session.
Practical Scenarios
Forcefully Killing All Detached Screens
In some scenarios, such as a server reboot or maintenance window, you might want to kill all detached screen sessions. This can be done using a combination of shell commands to filter and kill these sessions:
This command sequence lists all sessions, selects detached sessions, extracts their PIDs, and kills them.
Common Issues and Troubleshooting
Sometimes, killing a screen session might not succeed due to permissions or because the session is hung in a more severe state. In such cases, a more forceful kill may be required:
Using kill -9 sends a SIGKILL signal, which forcefully stops the process without waiting for it to close gracefully.
Summary Table
Here's a brief summary of key commands and their descriptions:
| Command | Description |
screen -ls | Lists all screen sessions. |
screen -X -S [session #] quit | Kills a specific detached screen session. |
kill [PID] | Kills a session by its PID. |
kill -9 [PID] | Forcefully kills a session by its PID. |
Combined grep and kill command | Kills all detached sessions. |
Conclusion
Understanding how to manage and terminate detached screen sessions is an essential skill for sysadmins and users who manage remote or long-running processes on UNIX-like systems. Properly killing unwanted screen sessions helps in resource management and ensuring that only necessary processes are running, thereby maintaining system performance and stability.
Remember to always check which sessions are running and their necessity before issuing kill commands, as terminating crucial system processes can lead to data loss or system instability.

