Kill all detached screen sessions

Gnu Screen

Gnu Screen Problem Overview


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.

Gnu Screen Solutions


Solution 1 - Gnu Screen

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

Solution 2 - Gnu Screen

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

Solution 3 - Gnu Screen

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.

Solution 4 - Gnu Screen

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
}

Solution 5 - Gnu Screen

If the screens are dead, use:

screen -wipe

Solution 6 - Gnu Screen

'[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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRose PerroneView Question on Stackoverflow
Solution 1 - Gnu ScreenMilind ShahView Answer on Stackoverflow
Solution 2 - Gnu ScreenEdward NewellView Answer on Stackoverflow
Solution 3 - Gnu ScreenRose PerroneView Answer on Stackoverflow
Solution 4 - Gnu Screenjmc1337View Answer on Stackoverflow
Solution 5 - Gnu ScreenTransientObjectView Answer on Stackoverflow
Solution 6 - Gnu ScreenkrokoView Answer on Stackoverflow