2016-02-08 64 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也可能有用。

相关问题