终止多个同名的 GNU 屏幕会话

终止多个同名的 GNU 屏幕会话

有时我的脚本会出错,并且会创建多个同名的屏幕会话。有没有一种方法可以在一个命令中杀死所有具有该名称的文件?

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)

除了这些屏幕之外,还有其他屏幕正在运行,但我不想杀死它们。

答案1

如果没有其他screen会话正在运行,您可以使用“硬”方式,只kill使用killall screen.

如果你想变得友善,你可以迭代你的屏幕会话列表并一个接一个地杀死它们:

# 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.

答案2

screen -ls "$SESSION_NAME"在制表符缩进行上列出匹配会话的全名。因此,您可以迭代这些行,提取全名,并对quit每个匹配的名称调用命令。

SESSION_NAME='haydoz-script'
screen -ls "$SESSION_NAME" | (
  IFS=$(printf '\t');
  sed "s/^$IFS//" |
  while read -r name stuff; do
      screen -S "$name" -X quit
  done
)

答案3

这是一行。您需要更换your_session为所需的。

screen -ls your_session | grep -E '\s+[0-9]+\.' | awk -F ' ' '{print $1}' | while read s; do screen -XS $s quit; done

答案4

附加到屏幕,然后运行另一个屏幕命令:

screen -x "Name" -x -X kill

相关内容