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
  • 4
  • This question has already been answered. You will find your answer here: http://serverfault.com/questions/96406/kill-a-screen-but-not-all-screens – GeorgeB May 30 '13 at 21:47
  • No, That is not the same. I want to kill all screens with the same name at the same time. That question kills one screen at a time. I want a script to kill them at the same time. – Runner May 30 '13 at 22:00
  • "all screens with the same name" is vague. I presume you mean "all screens named 'screen', but not named 'realscreen'" – Dennis Williamson May 30 '13 at 22:15
  • Yes, that is what I mean. I want to kill the screen if there are more than one with the same name – Runner May 30 '13 at 22:18
  • Use grep on the screen -ls output, then pipe the output through the command to kill one screen. – Jenny D May 31 '13 at 07:29

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,334
  • 1
  • 46
  • 69
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.

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
  • Will this affect anything else? I am root, so I do not want to kill processes that I need running. – Runner May 30 '13 at 22:40
  • @Runner: It will kill processes that match the regex you supply. You should make sure that it only matches processes you want to kill. The more specific your regex, the lower the chances that it will kill a false positive. Anchoring the regex will improve the selectivity. `pkill -f '^SCREEN.*\ – Dennis Williamson May 31 '13 at 00:33