2016-02-08 59 views
0

我有一個運行多個實驗(實現時機等)的bash腳本,但如果它已經運行,我不想運行相同的實驗。Bash:檢查腳本是否已經在這臺計算機上運行

實驗已經運行if if same計算機產生一個輸出文件,其中包含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}'來獲取計算機的MAC地址,然後將輸出文件存儲在一個目錄中,例如, statistics/ff:ff:ff:ff:ff/experiment1.csv,但((假設安裝了ip)是否保證所提到的命令會返回一個MAC地址?或者還有其他方法來從bash腳本中唯一標識一臺計算機嗎?

+1

也許你可以使用主機名而不是IP或MAC地址? – Pierre

+0

我認爲,對於我的用例來說,這應該足夠了! – YnkDK

回答

0

使用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也可能有用。

相關問題