I accidentally created over 1000 screens. How do I kill them all with one command? (Or a few)
8 Answers
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
-
2It's not recommended to use SIGKILL in this case. SIGTERM would be a much better choice.Marco– Marco2013-10-10 22:31:27 +00:00Commented Oct 10, 2013 at 22:31
-
Hi, Rahul Patil. why screen is not listed in
Topuser15964– user159642016-04-30 01:03:08 +00:00Commented Apr 30, 2016 at 1:03 -
What if I don't have permissions? Do I have to manually go into and exit all the screens individually?hipoglucido– hipoglucido2017-06-21 15:39:10 +00:00Commented Jun 21, 2017 at 15:39
-
1In OSX (also potentially in Linux), ongoing processes running inside the screen are "orphaned".f01– f012022-12-06 03:26:14 +00:00Commented Dec 6, 2022 at 3:26
-
@marco why not? Also,
pkillalready sendsSIGTERM, notSIGKILL. Ditto forkillall.ijoseph– ijoseph2025-08-04 17:54:23 +00:00Commented Aug 4 at 17:54
If the screens are dead, use:
screen -wipe
-
This would be good if it worked for the use case in the above question. Instead it:
Do nothing, just clean up SockDir [on possible matches].ijoseph– ijoseph2025-08-04 17:51:28 +00:00Commented Aug 4 at 17:51
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.
-
17
screen -ls | grep '(Detached)' | awk '{print $1}' | xargs -I % -t screen -X -S % quitworked better for me.whereisalext– whereisalext2019-09-22 23:24:25 +00:00Commented Sep 22, 2019 at 23:24
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
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 \).
-
1Try 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– Railgun22013-10-11 01:44:38 +00:00Commented Oct 11, 2013 at 1: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– Railgun22013-10-11 01:49:00 +00:00Commented Oct 11, 2013 at 1:49
-
1Well 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– BrainStone2013-10-11 10:14:26 +00:00Commented Oct 11, 2013 at 10:14 -
How exactly does one create an infinite loop by accident?ahron– ahron2022-07-04 18:13:11 +00:00Commented Jul 4, 2022 at 18:13
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.
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)'
-
This one works.WestCoastProjects– WestCoastProjects2020-02-18 18:40:23 +00:00Commented Feb 18, 2020 at 18:40
screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs killscreen -ls | grep "<name>" | cut -d. -f1 | tr --delete "\t" | xargs kill -9; screen -wipe; screen -ls;