206

I'm trying to kill a screen session. I noticed a lot of other related questions, but none of those answers are working for me. I am trying to kill the following session:

screen -ls
There is a screen on:
    23520.pts-6.porkypig    (09/30/2013 02:49:47 PM)    (Detached)
1 Socket in /var/run/screen/S-root.

screen -r 23520.pts-6.porkypig

Now I am in the session. According to the documentation:

http://www.gnu.org/software/screen/manual/screen.html#Quit

I am supposed to press "control a" and then "control \". I do that and nothing happens.

Another solution said to press Ctrl+a and type :quit. However, again it doesn't do anything. In fact, pressing control+a, absolutely nothing happens afterwards except a message "No Other Window"

4
  • 3
    in screen v4.x, to kill all sessions, ps aux | grep "SCREEN" | awk '{print $2}' | xargs kill Nov 28, 2014 at 7:33
  • 3
    For the record, you can just do screen -r porkypig or screen -r 23520, rather than having to include the full string. Screen names are supposed to make things easier, not more complicated.
    – Mikkel
    Sep 26, 2016 at 14:52
  • we can also use the exit command to terminating screen Jan 20, 2021 at 10:58
  • For the record, you could also do screen -r if there's only one screen listed Apr 12, 2023 at 20:42

13 Answers 13

250
  1. Identify the name of the session:
 $ screen -ls
  1. Close a session:
$ screen -XS <session-id> quit
  • Rationale: -X = Execute command, -S session PID to execute on
  • Example: screen -XS 20411 quit
  • Source: innaM's Answer
7
  • 1
    This was pretty simple. You can simplify it even more by combining the -X -S with -XS
    – AJ Acevedo
    Nov 10, 2016 at 3:34
  • 5
    You should give credit to innaM answer if you copy it May 1, 2017 at 14:47
  • 1
    This answer is most probably the most straightforward, quick yet efficient one.
    – avia
    Jul 11, 2021 at 10:01
  • Is much better than the accepted answer, and it's working! Sep 13, 2022 at 22:10
  • 1
    i like this one too. plus can always throw it in a loop to get rid of all sessions if needed: for session in $(screen -ls | grep -oP '\d+\.\w+' | cut -d. -f1); do screen -S "${session}" -X quit; done. May 8, 2023 at 6:46
223

first you need to re attach to the screen session
screen -r 23520 as you have done. Then press ctrl + a and then a k and press y when it asks if you really want to kill the session

Source

5
  • 2
    This works if there is a problem with a process running inside the screen, but not if there is a problem with screen itself. That is the case 99% of the time, and has the added benefit of only affecting a single window within the screen rather than terminating all windows.
    – Mikkel
    Sep 26, 2016 at 14:51
  • 2
    It is to kill a window, not a session
    – Eric Wang
    Aug 10, 2019 at 0:47
  • Is there a way to kill the session without having to answer the prompt? I'd rather skip the y part.
    – birgersp
    Feb 26, 2021 at 7:32
  • 1
    Ctrl + a then UPPERCASE K, then y Mar 22, 2022 at 10:10
  • 2
    Ctrl + A + K doesn't work for me.
    – Peyman
    Nov 25, 2022 at 17:48
17

This command will kill all screen sessions, if that is desired:

So with all those official suggestions, I have one here that i feel is easier, and just as effective, and kind of more straight forward:

pkill screen

Who wants to go into an unknown and un-needed screen just to press in a couple commands that most might barely remember? This avoids going into it at all, and kills it straight off.

Plus, if you have more than one, this will take them all in one fell swoop.

2
  • 7
    Note that this will kill all running screens, which may or may not be what you want.
    – Mikkel
    Sep 26, 2016 at 14:57
  • 2
    Not a good choice. I've been doing it for some time. Sometimes it causes bad behaviors (e.g. logging out from your user account immediately). Also, as @Mikkel mentioned, it could cause to close all your screens, which would not be what you want. Sometimes you need to keep running some (e.g. some are running by system) and stop some other screens. Sep 4, 2018 at 5:14
12

Like you, I wanted to kill my screen session and found the documentation unhelpful. Convinced that there must be a keyboard shortcut, I found that
ctrl + a then \
works

I then get the prompt: "Really quit and kill all your windows [y\n]"

I am not sure why the documentation says ctrl + a then ctrl + \. That doesn't do anything for me.

10

This will kill all the detached screens:

screen -ls | grep detached | cut -d. -f1 | awk '{print $1}' | xargs kill

This will kill all screens, attached or detached

screen -ls | grep pts | cut -d. -f1 | awk '{print $1}' | xargs kill
2
  • Is the awk step really necessary? Jul 27, 2022 at 8:05
  • I am an grep/awk/xargs noob and always will be. But based on your answer I built my own shell alias. Just in case this may be useful for someone: alias killscreen='screen -ls | grep -i attached | awk '\''{print $1}'\'' | xargs -L1 bash -c '\''screen -XS $0 quit'\''' (kills all attached sessions) Dec 25, 2023 at 12:23
5

You can use this to kill a session

screen -X -S <name> kill
4

we can also use the exit command to terminating screen

1
  • Easiest and most straightforward approach! You will, of course, first need to reattach to the screen session.
    – gillesC
    Jul 16, 2021 at 8:36
3

I encountered this problem when updating screen. The screen command would hang when attempting to reattach the session, regardless of how many -D or -R I added to the command. However, screen -ls conveniently provides the pid of the session, allowing you to intervene using the following:

10:42 user ~ $ screen -ls
There is a screen on:
        5730.my_screen     (Detached)
1 Socket in /tmp/screens/S-user.

10:42 user ~ $ sudo kill 5730

10:43 user ~ $ screen -ls
No Sockets found in /tmp/screens/S-user.

(This is similar to Brian Thomas's answer, but his will kill all running screen sessions, which may not be what you want if you have multiple screens open but only one misbehaving.)

1
  • After 6 hours breaking my head all over internet... yours was the only answer that worked. Thanks mate!
    – kRazzy R
    Sep 30, 2017 at 6:43
3

You can find the process id of the attached running screen. I found it same as the session id which you can get by command:
screen -ls
And you can use following command to kill that process:
kill [sessionId] or
sudo kill [sessionId]
You can kill the screen even if it is attached on some other terminal window.

0

Press ctrl+d to kill screen window. Repeat this until you kill all screen windows. Once you ran out all windows screen will kill the session and terminating. Shortest solution if you not having many windows

0

You can just simply type exit while in a recording mode, I found out it to be most convenient as it directly exits the running screen.

0

Simply, use the exit command inside a screen window and if you have a running process press control + z before that.

1
  • This doesn't answer the question (how to kill a screen session, as opposed how to exit normally) and it doesn't expand on the other answers. A good answer might cover the normal exiting procedure and then document a few ways you can kill screen if that doesn't work.
    – Coljac
    Mar 2, 2022 at 22:38
0

I believe what is being asked is how to 'quit' a screen session from within screen itself. This would kill all open terminals in the session and leave the user at the prompt for the parent terminal. The session can be suspended from within using CTRL-A, CTRL-Z. But this creates a a problem I'm trying to avoid which is leaving a bunch of opened screen sessions over time and then not knowing which ones are truly active and which ones I should have closed.

After lots of looking, I believe there is no way to do this. Killing them using the PID method is ok, but if there are many sessions open, some of which I might be using, I don't always know which ones to kill.

So what I do when I'm truly done with a screen session is to repeatedly hit CTRL-d to close each terminal individuly until the session completely exits.

This is the best solution I've found.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .