簡體   English   中英

Bash:檢查腳本是否已在此計算機上運行

[英]Bash: Check if script already ran on this computer

我有一個bash腳本,可以運行多個實驗(實現的時序等),但是如果已經運行,我不想運行相同的實驗。

如果同一台計算機產生了包含280行的輸出文件,則已經進行了一項實驗。

我現在要做的是以下(對於所有實驗):

# Remove the prefix
tmp=${3#*/}
# Remove the suffix
instance=${tmp%.*}
# Set the output path
output_path=statistics/experiment-$1-$2-${instance}.csv
if [ -f ${output_path} ];
then
    # The output file exists, check if it contains 280 lines
    LC=`wc -l ${output_path} | cut -f1 -d' '`
    if [[ ${LC} == 280 ]]; then
        # It did, thus we are done
        echo "$(date -R) [SKIPPING ] Instance already processed for ${1} (${2}) on ${3} - Skipping experiment!"
        # Do not do anything else
        exit 0
    fi
    # The experiment was stopped prematurely, rerun it
    echo "$(date -R) [RERUNNING] Instance not fully processed for ${1} (${2}) on ${3} - Rerunning experiment! (${LC}/280 runs)"
    # Remove old file
    rm -f ${output_path}
fi
# Run the experiment
echo "$(date -R) [RUNNING  ] Running experiment for ${1} (${2}) on ${3}"
start=$(date +%s)
./bin/Experiment --algorithm $1 --dbms $2 --instance_path $3 > ${output_path}
total=$(($(date +%s)-${start}))
echo "$(date -R) [FINISHED ] Finished experiment for ${1} (${2}) on ${3} in ${total} seconds."

但這不會檢查實驗是否在同一台計算機上運行。 我最初雖然是使用ip link show eth0 | awk '/ether/ {print $2}' ip link show eth0 | awk '/ether/ {print $2}'以獲取計算機的MAC地址,然后將輸出文件存儲在目錄中,例如statistics/ff:ff:ff:ff:ff/experiment1.csv ,但是(鑒於ip是否已安裝)是否保證上述命令將返回MAC地址? 還是通過bash腳本唯一地標識計算機?

使用dmidecode 它檢查系統並返回制造商設置的序列號。 您有幾種選擇。 您可以使用dmidecode -t 2返回類似:

           Handle 0x0002, DMI type 2, 8 bytes.  Base Board Information
           Manufacturer: Intel
           Product Name: C440GX+
           Version: 727281-001
           Serial Number: INCY92700942

在上面的示例中,您可以看到主板的序列號,但看不到系統。 為此,您可以使用dmidecode -t 1 解析所有內容可能很麻煩,因此只需將其通過sha256sum傳遞並獲得簡單的哈希即可。

使用dmidecode -s system-uuid也可能有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM