68

When I execute screen -ls, I see the following. How can I kill all the detached sessions?

There are screens on:
        84918.ttys002.ros-mbp   (Detached)
        84944.ttys008.ros-mbp   (Detached)
        84970.ttys013.ros-mbp   (Attached)
        84998.ttys002.ros-mbp   (Detached)
        85024.ttys002.ros-mbp   (Detached)
5 Sockets in /var/folders/86/062qtcyx2rxbnmn8mtpkyghs0r0r_z/T/.screen.
double-beep
  • 5,031
  • 17
  • 33
  • 41
Rose Perrone
  • 61,572
  • 58
  • 208
  • 243

6 Answers6

111

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

Kill only Detached screen sessions (credit @schatten):

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

Geoffrey Hale
  • 10,597
  • 5
  • 44
  • 45
Milind Shah
  • 1,127
  • 1
  • 7
  • 2
  • 40
    Good solution, thanks. But it also kills attached session. I used this one `screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs kill` – schatten Nov 14 '13 at 18:54
  • @schatten Could you explain the working of the command separately per pipe? – easytarget Jul 20 '14 at 16:55
  • 7
    @MusséRedi `screen -ls` – does not start a new screen, but lists all screen sessions; `grep Detached` – detached sessions are marked as 'Detached' in the previous output; `cut -d. -f1` - splits every string by "."(-d.) and then select only the first part (-f1), this way we have only pid with possible leading spaces; `awk {print $1}` – it reads the input line and splits it by spaces, so basically in this case it just removes leading spaces; `xargs kill` – runs `kill` cmd with appended arguments from stdin, so for every line you would get a `kill `. – schatten Jul 30 '14 at 00:47
  • 2
    If you're on a Linux box, not Apple, you'll need `screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs -r kill` to prevent the command erroring if there are no current screens running (especially useful in bash scripts) – Richard Jul 29 '16 at 09:06
  • Amazing, works perfectly fine! – phi Jun 05 '23 at 22:04
28

Here's a solution that combines all the answers: Add this to your .bashrc or .bash_profile:

killscreens () {
    screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs kill
}
  • this is a convenient function, easy to remember
  • kills only the detached screens, to stop you from doing something dumb
  • remember to open a new bash terminal or run source .bashrc to make killscreens available

Thanks to @Rose Perrone, @Milind Shah, and @schatten

Edward Newell
  • 17,203
  • 7
  • 34
  • 36
10

Include this function in your .bash_profile:

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

To run it, call killd. This will kill all screen sessions, detached or not.

Flimm
  • 136,138
  • 45
  • 251
  • 267
Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
3

Combining Edward Newell's and Rose Perrone's solutions into a more readable and "screen" like solution.

Add below to your .bashrc or .bash_profile.

# function for killing all detached screen sessions
killds() {
    detached_sessions=$(screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}')
    for s in ${detached_sessions}
    do
        screen -S "${s}" -X quit;
    done
}
jmc1337
  • 91
  • 1
  • 6
1

If the screens are dead, use:

screen -wipe
0
'[0-9]\{3,\}'

in case of

There is a screen on:
20505.blabla    (03/05/2014 22:16:25)   (Detached)
1 Socket in /var/run/screen/S-blabla.

will match both 20505 and 2014, where quitting 2014 will return "No screen session found."

[0-9]\{3,\}\.\S*

might work.

I've always encountered pattern 20505.name, where name is either host name or session name if screen was launched with -S flag. Works on OS X and Debian, might not be universal.

kroko
  • 131
  • 2
  • 6