0

I have a server, with a few screens started. This is what shows when I use screen -ls:

 There are screens on:
    12811.screen    (Detached)
    2061.screen (Detached)
    7055.screen (Detached)
    11746.screen    (Detached)
    11740.realscreen    (Detached)
    11740.screen    (Detached)
6 Sockets in /var/run/screen/S-root.

I have been trying to quit all screens at the same time that have the same name. I know I can quit all screens at once with killall screens, but this kills every screen, and I just want to quit the ones with the same name.

Is there a bash file that I would be able to use, that would loop through all the screens and quit them? Or is there another way I can quit all screens with the same name?

Runner
  • 23

2 Answers2

1

Something like this would do the trick, by parsing out the screen -ls output, matching only the screens with the same name and sending them a kill:

SCREENSTOKILL="screen"
screen -ls | egrep "\.${SCREENSTOKILL}[[:space:]]+\(Detached\)" | cut -d. -f1 | xargs kill

Or you could grab all the parent screen process IDs (child processes on my version of screen have a full name in all caps), exclude the one process you want to ignore, and then kill the rest:

pgrep -f screen | grep -v '11740' | xargs kill
freiheit
  • 14,844
0

Try this:

pkill -f 'SCREEN.*\<screen\>'

It looks for all screen sessions with "screen" as a separate word in the full process command line. It will kill processes such as the first and third processes, but not the second, as shown in this example output of ps:

dennis   25514  0.0  0.1   4216  1364 ?        Ss   17:04   0:00 SCREEN -S screen
dennis   25609  0.0  0.1   4216  1364 ?        Ss   17:04   0:00 SCREEN -S realscreen
dennis   25702  0.0  0.1   4216  1360 ?        Ss   17:04   0:00 SCREEN -S screen

If you had a screen session named "real screen" or "real.screen" it would also kill them. However, you can use as specific a regex as necessary.