Bash

我們如何根據它們附加或分離的屬性或它們的 ID 來終止螢幕會話?

  • December 28, 2019

問題:

我們在執行實驗時意外創建了近 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

在標準 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。

引用自:https://unix.stackexchange.com/questions/559172