「flock」在桌上型電腦上隨機失敗,但在筆記型電腦上卻沒有,可能是硬體有缺陷嗎?

「flock」在桌上型電腦上隨機失敗,但在筆記型電腦上卻沒有,可能是硬體有缺陷嗎?

我正在使用下面的腳本對flock.

在我的筆記本上,它總是運作得很好。
但是當我在桌上型電腦上運行它時,它可能會隨機失敗,讓兩個實例同時運行(獲得相同的鎖!)。
兩者都運行 ubuntu 20.04。

所以我懷疑我的桌上型電腦有硬體問題,可能是RAM?

所以,我的問題是,根據群體錯誤行為給我的提示,我如何追蹤/確定哪個硬體部件有缺陷?
我的意思是,哪群人可以指出哪些硬體有缺陷(或可能品質低劣)?

我如何確認問題:
我得到所有具有與完整路徑腳本名稱相符的子命令的叢集進程(這就是叢集應作為子命令執行的命令)。
另外,我記錄每個腳本完成的工作,並且在該日誌上有兩個或多個實例的同時工作。僅當取得鎖定時才會記錄工作。
我是否無法正確配置叢集來提供獨佔/唯一鎖?
我現在也在另一個終端上使用它:
while true;do date;lslocks |grep flock;sleep 1;done

劇本: cat >flock.tst.sh;chmod +x flock.tst.sh

#!/bin/bash

LC_NUMERIC=en_US.UTF-8

# HELPERS:
#trash /tmp/flock.tst.log;for((i=0;i<20;i++));do flock.tst.sh&:;done #run this on shell
#pkill -fe flock.tst.sh #end all concurrent children trying to acquire the lock
#while true;do date;lslocks |grep flock;sleep 1;done #use this to check if there is more than one lock acquired, check also the log file to confirm it, and if there is two subsequent WORK on the terminal log, it means a problem happened too

: ${bDaemonizing:=false}
: ${bReport:=false} #help use this to show more log, but will be harder to read it.
: ${bCheck:=false} #help this will let the script check if there is more than one instance working, but it may be slow and make it more difficult to let the problem happen
if ! $bDaemonizing;then
  echo "this IS a daemon script, only one instace runnable"
  flSelf="`realpath $0`"
  
  #set -x 
  while ! bDaemonizing=true flock --timeout=$(bc <<< "0.2+0.0$RANDOM") "$flSelf" "$flSelf" "$@";do
    if $bCheck;then
      strParents="$(nice -n 19 pgrep -f "^flock --timeout=.* $flSelf $flSelf" |tr '\n' ',' |sed -r 's"(.*),"\1"')"
      if [[ -n "$strParents" ]];then
        anDaemonPid=( $(nice -n 19 pgrep --parent "$strParents" -f "$flSelf") )
        if((${#anDaemonPid[*]}>1));then echo "ERROR: more than one daemon, flock failed!? :(";ps --no-headers -o ppid,pid,cmd -p "${anDaemonPid[@]}";fi
        if $bReport && ((${#anDaemonPid[*]}==1));then echo "$$:Wait daemon stop running or 'kill ${anDaemonPid[0]}'";fi #could be: "already running, exiting.", but the new instance may have updated parameters that work as intended now.
      fi
    fi
  done
  
  exit #returns w/e flock does
fi

echo "$$:Work:`date`"
for((i=0;i<60;i++));do
  echo "`\
    echo $i;\
    date;\
    ps --no-headers -o ppid,pid,stat,state,pcpu,rss,cmd -p "$PPID";\
    ps --no-headers -o ppid,pid,stat,state,pcpu,rss,cmd -p "$$";\
  `" >>/tmp/flock.tst.log
  
  sleep 1
done
echo "$$:Done:`date`"


一些結果:
如您所見,進程 1458428(flock:1458427) 和 1438949(flock:1438941) 同時取得了排他鎖:(

日誌檔案(由腳本建立)顯示了這個問題(在第 50 秒,發生了另一個不應該取得鎖的實例的第二個日誌):

2
Fri 22 Jan 17:03:16 -03 2021
1438953 1458427 S    S  0.0   784 flock --timeout=.22377 /home/myUser/bin/flock.tst.sh /home/myUser/bin/flock.tst.sh
1458427 1458428 S    S  0.0  3852 /bin/bash /home/myUser/bin/flock.tst.sh
50
Fri 22 Jan 17:03:16 -03 2021
1438925 1438941 S    S  0.0   788 flock --timeout=.229687 /home/myUser/bin/flock.tst.sh /home/myUser/bin/flock.tst.sh
1438941 1438949 S    S  0.0  3900 /bin/bash /home/myUser/bin/flock.tst.sh

lslockdate顯示了這一點(腳本上的while作為提示):

Fri 22 Jan 17:03:16 -03 2021
flock           1459088 FLOCK  1.8K WRITE* 0          0          0 /home/myUser/bin/flock.tst.sh
flock           1458427 FLOCK  1.8K WRITE  0          0          0 /home/myUser/bin/flock.tst.sh
flock           1438941 FLOCK  1.8K WRITE  0          0          0 /home/myUser/bin/flock.tst.sh (deleted)
Fri 22 Jan 17:03:17 -03 2021
flock           1459616 FLOCK  1.8K WRITE* 0          0          0 /home/myUser/bin/flock.tst.sh
flock           1458427 FLOCK  1.8K WRITE  0          0          0 /home/myUser/bin/flock.tst.sh
flock           1438941 FLOCK  1.8K WRITE  0          0          0 /home/myUser/bin/flock.tst.sh (deleted)

終端日誌(產生了許多並發實例)顯示了以下內容:

1438949:Work:Fri 22 Jan 17:02:23 -03 2021
1458428:Work:Fri 22 Jan 17:03:14 -03 2021
1438949:Done:Fri 22 Jan 17:03:26 -03 2021
1476209:Work:Fri 22 Jan 17:04:02 -03 2021
1458428:Done:Fri 22 Jan 17:04:17 -03 2021

答案1

假設:就在事情發生之前,您儲存了腳本。您的編輯器儲存了一個副本,然後將該副本移至原始路徑。看Vim 是如何做到的為什麼

實際上,舊鎖仍然保留在舊文件(inode)上。這如 的…/flock.tst.sh (deleted)輸出所示lslocks。但是flock您的腳本(它的另一個實例?)一直使用該路徑產生。當路徑開始指向另一個索引節點時,一些新的索引節點flock被允許取得新的鎖定。這如 的…/flock.tst.sh輸出所示lslocks

兩個鎖是獨立的,它們不能互相阻塞,因為它們使用了不同的文件。 Flock 按設計工作。

實際上,我可以透過運行腳本的兩個實例(因此一個獲得鎖定,另一個不斷嘗試獲得它)來重新創建您的結果,然後使用打開它並立即vim寫入 ( )。:w或者:

cp flock.tst.sh foo
mv foo flock.tst.sh

相關內容