Killing multiple GNU screen sessions with the same name

gnu-screenkill

Sometimes my script bugs out and multiple screen sessions with the same name get created. Is there a way to kill all of these with this name in one command?

Rx:/scripts# screen -S haydoz-script -X quit
There are several suitable screens on:
        13372.haydoz-script     (09/10/11 07:44:05)     (Detached)
        12287.haydoz-script     (09/10/11 07:30:45)     (Detached)
        12230.haydoz-script     (09/10/11 07:17:42)     (Detached)
        7824.haydoz-script      (09/09/11 11:42:26)     (Detached)
        7764.haydoz-script      (09/09/11 11:35:40)     (Detached)
        15753.haydoz-script     (09/04/11 15:00:27)     (Detached)
        15744.haydoz-script     (09/04/11 15:00:26)     (Detached)

There are other screens running besides these ones that I do not want to kill.

Best Answer

If there are no other screen sessions running you can use the "hard" way and just kill them with killall screen.

If you want to be nice you can iterate over your list of screen sessions and kill them one after another:

# screen -S foo && screen -S foo
[detached]
[detached]
# screen -ls
There are screens on:                                                                                                                                                                                      
        8350.foo        (Detached)                                                                                                                                                                         
        8292.foo        (Detached)                                                                                                                                                                         
2 Sockets in /tmp/screens/S-joschi.                                                                                                                                                                        

# This is the interesting line. Just replace "foo" with the name of your session(s)
# for session in $(screen -ls | grep -o '[0-9]*\.foo'); do screen -S "${session}" -X quit; done
# screen -ls
No Sockets found in /tmp/screens/S-joschi.