Open In App

How to Kill a Detached screen Session in Linux

Last Updated : 22 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The screen session is like virtual windows, And in Linux it works like a multiplexer terminal where we can create more than one window and work on that. we can create multiple windows inside one session. We called it virtual because the process will continue to run when their window will not be visible even if you have disconnected. Now, if we are working on more than one session in a virtual way then we also may need to kill them. So let’s see how we can kill the screen sessions. While using the GNU screen or screen session,  We can exit from the detached session that needs cleanup. In this article, we will talk about many options for killing a detached screen session.

Before going to discuss how to kill the existing session, let’s go for listing the existing sessions.

Listing Sessions

First, let’s create a couple of screen sessions by using the below command.

$ screen -dmS session_1
$ screen -dmS session_2

This will create two sessions named session_1 and session_2.

Notice that we have not attached this session because we have used -d. Now let’s see the sessions that we have created.

For listing the created sessions, run the below command.

Command:

$ screen -list

Output:

Our two sessions show up:

 

Next, let’s see how to kill these sessions.

There are two methods to kill the session:

  • Method 1: Attach and kill the screen session.
  • Method 2: Kill the screen session without attaching.

Let’s discuss both methods

Method 1: Attach and kill the screen session.

First attach  session_1  that we have created above, For that run the below command.

$ screen -r session_1

Now our command prompt is now inside our session. So we can just type:-

$ exit

The session will start terminating, and we can see  [screen is terminating] in the output.

Now we have only one session left. That is session_2 because we have killed the session_1.

Output:

 

Note:  If the screen session had more than one window, we have to type exit at every window before the screen session would end.

Method 2: Kill the screen session without attaching.

In this method, we will not attach the session. Let’s look up the way to kill the screen session without attaching them.

First, create a couple of screen sessions to kill.

$ screen -dmS session_3
$ screen -dmS session_4

Now, our two screen sessions have been created. Let’s list the session by using the below command.

$ screen -list

Output:

 

Now, we will use the screen command argument -X to send the command in a running screen version. The -S will allow us to specify the session that we want to kill. And for killing that particular session, we will add the quit command.

$ screen -S session_3 -X quit

Now, the screen session_3 has been killed. Let’s list our current sessions by using the below command.

$ screen -list

Output:

 

We can see that we have only session_4 because we have killed the session_3.

Conclusion:

In this article, we have discussed multiple methods to kill a Detached screen Session in Linux. In the first method, first, we have to attach the screen session, and then we can kill that. In the second, method we have seen that we can kill the screen session without attaching that.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads