138

I accidentally created over 1000 screens. How do I kill them all with one command? (Or a few)

BrainStone
  • 3,654
  • 3
    run --> screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs kill – sactiw Nov 12 '16 at 10:26
  • Good idea @sactiw. I modified it to work with my named sessions, and tweaked the commands a bit for preference, cleaning the sessions out completely in case they are 'stuck' like they were for me, and listing again for the user as a sanity check: screen -ls | grep "<name>" | cut -d. -f1 | tr --delete "\t" | xargs kill -9; screen -wipe; screen -ls; – Pysis Aug 09 '17 at 14:37
  • 7
    How did you accidentally create 1000 screens? – duhaime Apr 04 '18 at 00:06
  • 1
    @duhaime asking the real question. – ahron Jul 04 '22 at 18:11
  • I was using a loop and running different parameters for a model in each screen. It all happened so fast! – Adam_G Aug 17 '22 at 21:56

8 Answers8

171

You can use :

pkill screen

Or

killall screen

In OSX the process is called SCREEN in all caps. So, use:

pkill SCREEN

Or

killall SCREEN
Rahul Patil
  • 24,711
18

If the screens are dead, use:

screen -wipe
Adam_G
  • 413
13

Have recently begun to familiarize myself with awk I put together this and it served its purpose. I posted it since its quite easy to understand.

screen -ls | grep '(Detached)' | awk 'sys {screen -S $1 -X quit}'

Where screen -ls lists all current screens. grep 'pattern' filters out all matching rows. We can then get a handle for all detached screens and with awk sys {command} we can copy and paste together a command and execute it with sys, $1 refers to the first argument picked up by awk. Finally we execute the quit command with screen -X quit.

Eliassen
  • 131
7
str=$(screen -ls)  

array=$(echo $str|tr "." "\n")  

for V in $array  
do  
if [ $V -gt 0  ]  
        then screen -S $V -X quit  
fi  
done 
Archemar
  • 31,554
Don Wei
  • 71
5
for scr in $(screen -ls | awk '{print $1}'); do screen -S $scr -X kill; done
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
3

I'm a bit puzzled over how you managed to create 1000 "screens". Did you perhaps mean 1000 screen windows (1000 different terminal windows within a single screen session)?

If you meant 1000 windows within a single screen session, then a more elegant solution would be to quit screen using the command C-a \ (ctrl-a followed by \).

Railgun2
  • 235
  • 1
    Try executing screen 1000x within screen. What happens? You get 1000 screen windows. Yes, that's right, screen intelligently and mercifully doesn't spawn 1000 screen sessions. So if the OP had already started screen it should be far easier to accidentally start 1000 screen windows than to start 1000 screen sessions. – Railgun2 Oct 11 '13 at 01:44
  • Strange but the comment I was replying to appears to have disappeared. Anyway I'm letting the above comment stand for the record. – Railgun2 Oct 11 '13 at 01:49
  • 1
    Well it's actually pretty simple. Just make a infinite loop (by accident) and put something like this in there screen -m sleep 100000. This happened and I did not notice until it had already created over 1000 screen sessions. – BrainStone Oct 11 '13 at 10:14
  • How exactly does one create an infinite loop by accident? – ahron Jul 04 '22 at 18:13
1

The following command will terminate all inactive screens:

perl -e 'while (map { kill 9, [split /\./]->[0] } grep { /Detached/ } split /\n/, qx{screen -ls}) { sleep 1 } exec qw(screen -wipe)'
Kevdog777
  • 3,224
1

You can use the screen command itself to list all active screen sessions and then kill them one by one. Here's an example:

screen -ls | awk '{print $1}' | xargs -I{} screen -X -S {} quit

This will list all active screen sessions using the screen -ls command, extract the session IDs using awk, and then pass each session ID to the screen -X -S command to quit the session.