我们如何根据屏幕会话的附加或分离属性或 ID 来终止屏幕会话?

我们如何根据屏幕会话的附加或分离属性或 ID 来终止屏幕会话?

问题:

我们在进行实验时意外创建了近 80 个屏幕。

我们可以利用他们的ID一一消灭他们。但是,有没有一种方法可以使用脚本或命令组合来结束所有这些操作,以自动化该过程并节省时间?

There are screens on:
    31408.erpnext   (12/27/2019 03:13:14 PM)    (Attached)
    31396.erpnext   (12/27/2019 03:13:14 PM)    (Attached)
    31384.erpnext   (12/27/2019 03:13:14 PM)    (Attached)
    31372.erpnext   (12/27/2019 03:13:14 PM)    (Attached)
    31360.erpnext   (12/27/2019 03:13:14 PM)    (Attached)
    31348.erpnext   (12/27/2019 03:13:14 PM)    (Attached)
    31336.erpnext   (12/27/2019 03:13:14 PM)    (Attached)
    31324.erpnext   (12/27/2019 03:13:14 PM)    (Attached)
    31312.erpnext   (12/27/2019 03:13:14 PM)    (Attached)
    31300.erpnext   (12/27/2019 03:13:14 PM)    (Attached)
    31288.erpnext   (12/27/2019 03:13:14 PM)    (Attached)
    31276.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31264.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31252.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31240.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31228.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31216.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31204.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31192.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31180.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31168.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31156.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31144.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31132.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31120.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31108.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31096.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31084.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31072.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31060.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31048.erpnext   (12/27/2019 03:13:13 PM)    (Attached)
    31036.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    31024.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    31012.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    31000.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30988.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30976.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30964.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30952.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30940.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30928.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30916.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30904.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30892.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30880.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30868.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30856.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30844.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30832.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30820.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30808.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30796.erpnext   (12/27/2019 03:13:12 PM)    (Attached)
    30784.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30772.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30760.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30748.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30736.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30724.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30712.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30700.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30688.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30676.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30664.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30652.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30640.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30628.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30616.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30604.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30592.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30580.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30568.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30556.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30544.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30532.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30520.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30508.erpnext   (12/27/2019 03:13:11 PM)    (Attached)
    30496.erpnext   (12/27/2019 03:13:10 PM)    (Attached)
    30484.erpnext   (12/27/2019 03:13:10 PM)    (Attached)
    30471.erpnext   (12/27/2019 03:13:10 PM)    (Attached)
    30458.erpnext   (12/27/2019 03:13:10 PM)    (Attached)
    30446.erpnext   (12/27/2019 03:13:10 PM)    (Attached)
    30433.erpnext   (12/27/2019 03:13:10 PM)    (Detached)
    5061.erpnext    (12/23/2019 06:03:38 PM)    (Detached)
83 Sockets in /var/run/screen/S-erpnext.

我们想做的:

终止除 之外的所有内容5061.erpnext

到目前为止我们已经尝试过:

来源

#!/bin/bash
running_screens=$(screen -ls | grep -v "5061.erpnext")
echo "${running_screens}"

for i in {$running_screens}
do
    screens=$(${i} | grep ".")

    if [[ ${screens} != "" ]]; then
        echo "${i}"
    fi
done

答案1

在标准 shell 语法中:

cd /var/run/screen/S-erpnext
for screen in *
do
  case $screen in
    (5061.erpnext) : do nothing         ;;
    (*)            kill "${screen%%.*}" ;;
  esac
done

上述对该用户的屏幕目录的更改然后循环遍历该目录中的每个会话文件。如果会话是不是5061.erpnext,然后它杀死相应的PID(第一个句点之前的数字)。扩展${screen%%.*}从字符串末尾删除尽可能多的字符,直到找到句点。这具有剥离掉那些东西的效果不是PID。使用内置: do nothing函数:,它不执行任何操作,包括忽略任何参数,因此我对该行进行了自我注释,以表明我们这样做不是杀死这种情况下的任何 PID。

相关内容