
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Detach Screen from Another SSH Session
Introduction
In Linux systems, it is possible to open multiple terminal sessions over SSH (Secure SHell). This can be very useful when working on a remote server and you want to keep multiple terminal shell windows open at the same time. However, there may be situations where you need to force detach/remove a screen session that is running in another SSH session. This can happen if you accidentally leave a screen session running on a remote server and then close your terminal window, or if you want to end a screen session that someone else on your team has left it running. In this article, we will look at how to force detach a screen session from another SSH session in Linux.
Detaching/Pausing a SSH Screen Session
To detach a screen session, you will need to first connect to the remote server using SSH. Once you are logged in, you can list the available screen sessions by executing the screen command with -ls flag. This command will show you a list of all the screen sessions that are currently running on the server, along with their session names and process IDs.
For example, if you have two screen sessions running on the server, the output of the screen -ls command might look like this
There are 2 screens on: 12345.pts-0.server (Detached) 12346.pts-0.server (Detached) 2 Sockets in /var/run/screen/S-user.
To detach a screen session, we use the screen -d command followed by the session name or process ID. For example, to detach the screen session with a process ID of 44345, we use the following command
$ screen -d 44345
If the screen session is currently attached (or it is being used by another terminal window), you will need to use the "-D" flag to force it to detach. For example
$ screen -D 12345
Detaching All Screen Sessions
If you want to detach all screen sessions at once, you can use the screen -X command followed by the -S flag and the -Q flag. For example
screen -X -S . -Q quit
This will detach all screen sessions that are currently running on the server.
Using the screen Command in a bash Script
The screen command can be useful in scripts for automating tasks on a remote server. For example, you could use a script to start a screen session, run a command in the screen session, and then detach the screen session when the command is finished.
To use the screen command in a script, you can include it as a command line argument. For example, the following script will start a screen session, run the ls command, and then detach the screen session
#!/bin/bash screen -S myScrnsession -d -m ls
To attach the screen session and view the output of the ls command, you can use the screen -r command as shown in the previous paragraph.
$ screen -r myScrnsession
Attaching/Resuming a Detached SSH Screen Session
If you have detached a screen session and want to resume it later, you can use the screen command -r option as described in the previous section. However, if there are multiple detached screen sessions with the same name, you may need to use the -d flag to specify which session you want to attach to.
For example, to attach the screen session with process ID of 44345, you can use the following command
$ screen -r 44345
Alternatively, you can use the -d and -R flags to attach to the most recently detached screen session with the same name. This can be useful if you have multiple detached screen sessions and you are not sure which one you want to attach to.
$ screen -d -R 44345
Killing a SSH Screen Session
If you want to completely kill a screen session, you can use screen command with -X option followed by the -S flag and the session name or process ID. For example, to kill the screen session with a process ID of 44345, you can use the following command
$ screen -x -S 44345 quit
Conclusion
In this article, we have gone through the steps of forcefully detaching a screen session from another SSH session in Linux. We have also looked at how to attach and kill screen sessions. By using these commands, you can easily manage your screen sessions on a remote server and ensure that they are not left running unnecessarily.