Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

When using GNU screen, we can sometimes end up with detached sessions that need cleanup. In this quick tutorial, we’ll walk through a few options for killing a detached screen session.

2. Listing Sessions

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

% screen -dmS my_session_1
% screen -dmS my_session_2

This will create two sessions named my_session_1 and my_session_2. Notice we are not attached 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)

Next, let’s talk about how to kill these sessions.

3. Attach and Kill a screen Session

One way we can kill a screen session is to attach and then kill it. So, let’s attach to the first session we created above:

% screen -r my_session_1

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

% 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’d have to type exit (or CTRL+a k) at every window before the screen session would end. An easier alternative is the quit command:

CTRL+a \

(Note: you need to hold CTRL+a while hitting the \ key.) This prompts us with a confirmation:

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

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

The above-attached scenario is straightforward because we created the screen session in the same window. Now, if there’s another terminal or user attached to the session we want to kill, we need a different command to attach. In our current terminal window, create a new session:

% screen -S my_session_3

The -S will create the session and attach it to 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 this is now “Attached” because we are attached in our first terminal. In our second terminal, we’re going to force the session to detach from the first terminal window and attach to the second terminal. In the second terminal, type:

% screen -D -R my_session_3

Our second terminal is now in the screen session. Our first terminal shows a warning and is back at the terminal:

[remote power detached]

Now we can use exit as shown in the previous section.

4. 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.

First, let’s create 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)

We can now use the screen command argument -X to send a command to a running screen session. The -S will allow us to specify the session that will receive the command. So, to send a quit 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)

Lastly, we can always kill a screen session via OS commands. The numbers prepending the name are the PID of the screen session. To kill our last session, we can use kill:

% kill 19671

Checking our screen sessions now, we’ll see:

% screen -list
No Sockets found in /var/folders/wr/129xvd3dfecl/T/.screen.

5. Conclusion

In this article, we walked through various ways to kill a detached screen session.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!