863
votes

I learned from somewhere a detached screen can be killed by

screen -X -S [session # you want to kill] kill

where [session # you want to kill] can be gotten from

screen -ls

But this doesn't work. Anything wrong? What's the correct way?

5
  • 25
    "somewhere" is not always a good source of information. Try "man screen".
    – innaM
    Commented Oct 2, 2009 at 14:08
  • 27
    The correct command is screen -S sessionname -p 0 -X quit
    – jimmetry
    Commented Aug 29, 2013 at 2:27
  • killall -# screen [# = number of screens you want to kill]
    – CArnold
    Commented Aug 29, 2014 at 14:14
  • I simply use exit
    – TPArrow
    Commented Feb 16, 2018 at 13:15
  • CTRL + D when in screen is the easiest command.
    – Don King
    Commented Mar 28, 2019 at 17:50

11 Answers 11

1265
votes

"kill" will only kill one screen window. To "kill" the complete session, use quit.

Example

$ screen -X -S [session # you want to kill] quit

For dead sessions use: $ screen -wipe

7
  • 112
    type 'exit' (without the quotes) in ubuntu :P
    – mzalazar
    Commented Jan 23, 2013 at 12:48
  • 16
    exit works but needs to be typed into each screen that was opened. quit does not even work
    – kapad
    Commented Aug 19, 2013 at 22:35
  • 4
    @kapad actually quit works if you write it inline -X -S pid/sockname quit
    – Eduard
    Commented Jul 4, 2014 at 15:24
  • 20
    on OSX entering screen -X quit on any terminal terminates all active sessions
    – Michel
    Commented May 29, 2015 at 9:07
  • 5
    Ctrl + D also works
    – Yi Xin
    Commented Dec 6, 2017 at 13:28
497
votes

You can kill a detached session which is not responding within the screen session by doing the following.

  1. Type screen -list to identify the detached screen session.

    ~$ screen -list  
        There are screens on:  
             20751.Melvin_Peter_V42  (Detached)  
    

    Note: 20751.Melvin_Peter_V42 is your session id.

  2. Get attached to the detached screen session

    screen -r 20751.Melvin_Peter_V42
  3. Once connected to the session press Ctrl + A then type :quit

4
  • 20
    you can just use screen -r 20751 without the full name
    – Gal Bracha
    Commented Jul 9, 2013 at 12:47
  • 53
    or: Ctrl + a, k
    – laffuste
    Commented Nov 15, 2013 at 8:40
  • @laffuste 's comment worked for me, but quit and :quit lead to command not found on my remote Linux server (perhaps differences between versions of the OS or screen are to blame)
    – Hack-R
    Commented Mar 20, 2015 at 15:05
  • once attached to the screen, exit also works
    – pka32
    Commented Jan 13, 2017 at 7:17
145
votes

List screens:

screen -list

Output:

There is a screen on:
23536.pts-0.wdzee       (10/04/2012 08:40:45 AM)        (Detached)
1 Socket in /var/run/screen/S-root.

Kill screen session:

screen -S 23536 -X quit
1
  • 1
    'screen -ls' also works. ;) Commented Nov 21, 2016 at 7:00
113
votes

It's easier to kill a session, when some meaningful name is given:

//Creation:
screen -S some_name proc
// Kill detached session
screen -S some_name -X quit
2
  • Why screen -S and not screen -r? Commented Dec 2, 2017 at 0:52
  • 1
    This answer uses the name of the session, which is way more convenient than looking up the ID. Thank You! Commented Jul 9, 2018 at 18:29
43
votes

You can just go to the place where the screen session is housed and run:

 screen -ls

which results in

 There is a screen on:
         26727.pts-0.devxxx      (Attached)
 1 Socket in /tmp/uscreens/S-xxx. <------ this is where the session is.

And just remove it:

  1. cd /tmp/uscreens/S-xxx
  2. ls
  3. 26727.pts-0.devxxx
  4. rm 26727.pts-0.devxxx
  5. ls

The uscreens directory will not have the 26727.pts-0.devxxx file in it anymore. Now to make sure just type this:

screen -ls

and you should get:

No Sockets found in /tmp/uscreens/S-xxx.

2
  • 4
    This is the only solution that will work if the screen is "stuck", ie. not dead, but cannot be attached to.
    – redreinard
    Commented Jul 22, 2015 at 20:28
  • This helped me when the screen was utterly locked, but I did need to find and kill the actual process as well. ps aux | grep screen found the pid and I issued a kill to remove it. Depending on what you had running in your screen, you may have temp files and locks to clean up as well.
    – Lomky
    Commented Jan 9, 2017 at 16:29
29
votes
screen -wipe

Should clean all dead screen sessions.

2
  • 11
    What means dead? not running any program?
    – Shihao Xu
    Commented Jun 29, 2016 at 8:18
  • 1
    @ShihaoXu Dead means the session is unreachable and on localhost (socket connection is broken). - see gnu.org/software/screen/manual/screen.html
    – illnr
    Commented Jun 30, 2018 at 7:33
23
votes

add this to your ~/.bashrc:

alias cleanscreen="screen -ls | tail -n +2 | head -n -2 | awk '{print $1}'| xargs -I{} screen -S {} -X quit"

Then use cleanscreen to clean all screen session.

3
  • 11
    A simple one-liner: screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs kill
    – Ariel
    Commented Jul 28, 2014 at 17:41
  • 3
    Worked a treat, but had to modify it slightly to work on OSX: screen -ls | tail +2 | head -2 | awk '{print $1}'| xargs -I{} screen -S {} -X quit
    – Jonathan
    Commented Mar 15, 2015 at 4:39
  • Slight improvement:-screen -ls | grep Attached | cut -d. -f1 | awk '{print $1}' | xargs -I{} screen -d {} Commented Jul 18, 2015 at 5:22
21
votes

For me a simple

exit

works. This is from within the screen session.

3
  • 2
    also, ctrl+a :quit Commented Nov 18, 2016 at 19:22
  • 2
    also, ctrl+a, then K Commented Aug 14, 2017 at 21:51
  • Or just CTRL+D within a screen Commented Aug 1, 2018 at 14:34
16
votes

To kill all detached screen sessions, include this function in your .bash_profile:

killd () {
for session in $(screen -ls | grep -o '[0-9]\{5\}')
do
screen -S "${session}" -X quit;
done
}

to run it, call killd

1
  • 5
    Sometimes it's not 5 digits, so i use: killd () { for session in $(screen -ls | grep -o '[0-9]\+') do screen -S "${session}" -X quit; done }
    – Kostyantyn
    Commented May 20, 2013 at 11:58
7
votes
== ISSUE THIS COMMAND
[xxx@devxxx ~]$ screen -ls


== SCREEN RESPONDS
There are screens on:
        23487.pts-0.devxxx      (Detached)
        26727.pts-0.devxxx      (Attached)
2 Sockets in /tmp/uscreens/S-xxx.


== NOW KILL THE ONE YOU DONT WANT
[xxx@devxxx ~]$ screen -X -S 23487.pts-0.devxxx kill


== WANT PROOF?
[xxx@devxxx ~]$ screen -ls
There is a screen on:
        26727.pts-0.devxxx      (Attached)
1 Socket in /tmp/uscreens/S-xxx.
2
  • Wouldn't that just remove the socket, not kill the process behind it?
    – Martin C.
    Commented Jul 31, 2012 at 5:56
  • Is this method is risky from the point of view of creation of zomby processes ? Commented Oct 1, 2012 at 13:02
5
votes

Alternatively, while in your screen session all you have to do is type exit

This will kill the shell session initiated by the screen, which effectively terminates the screen session you are on.

No need to bother with screen session id, etc.

Not the answer you're looking for? Browse other questions tagged or ask your own question.