How to Kill a Detached screen Session on Linux


Introduction

Separate screen sessions are an excellent way to perform background processes on a Linux machine. However, sometimes it may be necessary to kill a separate screen session that is no longer necessary. In this article, we will show you how to remove a separate screen session on Linux using the command line.

When using the GNU screen tool, we can sometimes end up with separate sessions that need cleaning up. In this quick tutorial, we'll look at some options for deleting a disconnected screen session.

List of Sessions

Before we start discussing how to end existing sessions, let's first go through the list of existing sessions. First, let's set up a couple of sample screen sessions. In a bash shell, we type −

$ screen -dmS my_session_1
$ screen -dmS my_session_2

This will create two sessions called my_session_1 and my_session_2. Note that we are not connected to either (thanks to the -d option). Now, let's take a look at the sessions we created −

$ screen -list

Our two sessions show up −

There are screens on:
	84581.my_session_1   (Detached)
	76340.my_session_2   (Detached)

Attach and Kill a Screen Session

One way to remove a screen session is to attach it and then remove it. So, let's attach to the first session we created above −

$ exit

The session will end, and we should see −

[screen is terminating]

Now we only have one session left −

$ screen -list
There is a screen on:
	76340.my_session_2   (Detached)

If the screen session had more than one window, we would have to type exit (or CTRL+a k) in each window before the screen session ends. A simpler alternative is the quit command −

CTRL+a \

(Note − you need to hold CTRL+a while pressing the key.) This asks us for confirmation −

Really quit and kill all your windows [y/n]

We chose to close all session windows to end the screen session.

The attached scenario above is easy because we create the screen session in the same window. Now, if there is another terminal or user attached to the session that we want to remove, we need a different command to attach it. In our current terminal window, create a new session −

$ screen -S my_session_3

The -S will create the session and attach it. Now, let's open a second terminal window and list our screen sessions −

$ screen -list
There is a screen on:
	19643.my_session_3   (Attached)

Notice that it is now "Attached" because we are connected in our first terminal. In our second terminal, we will force the session to log out of the first terminal window and connect to the second terminal. In the second terminal, type −

$ screen -D -R my_session_3

Kill a Screen Session Without Attaching

As an alternative to attaching to a session to end it, let's look at a couple of ways to end a screen session without attaching it.

First, we believe a couple of sessions to kill −

$ screen -dmS my_session_4
$ screen -dmS my_session_5

Our two sessions are now created −

$ screen -list
There are screens on:
	19665.my_session_4   (Detached)
	19671.my_session_5   (Detached)

Now we can use the Argument -X screen command to send a command to a execution “screen” session. He will allow us to specify the session that the command will receive. So, to send an exit command to my_session_4, we would use −

$ screen -S my_session_4 -X quit

The screen -list shows our current sessions −

$ screen -list
There is a screen on:
	19671.my_session_5   (Detached)

Finally, we can always terminate a screen session via operating system commands. The numbers preceding the name are the PID of the session and can be used to terminate the session via the delete command.

$ kill -9 19671

This will end session 19671

Conclusion

In this article, we have shown various ways to end an independent screen session on Linux. We show how to check for separate display sessions, how to use the "screen -X -S" command to remove a separate display session, and how to use the "exit" command to remove a separate display session by linking it. We've also covered a method for ending a separate view session without connecting it using the "kill -9 PID" command. Remember that removing a video session will also remove all processes running within it. Therefore, be careful when using this command.

Updated on: 20-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements