[英]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.