YnkDK February 2016

Bash: Check if script already ran on this computer

I have a bash-script that runs several experiments (timing of implementations etc.), but I do not want to run the same experiment if it already ran.

An experiment already ran iff the same computer produced an output file, that contains 280 lines.

What I do now is the following (for all experiments):

# 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."

But this does not check if the experiment ran on the same computer. My initial though was to use ip link show eth0 | awk '/ether/ {print $2}' to get the MAC address of the computer and then store the output file in a directory, e.g. statistics/ff:ff:ff:ff:ff/experiment1.csv, but (given that ip is installed) is it guaranteed that the mentioned command would return a MAC address? Or are there otherways to uniquely identify a computer from a bash-script?

Answers


Allen February 2016

Use dmidecode. It examines the system and returns the serial number set by the manufacturer. You have several options. You could use dmidecode -t 2 which returns something like:

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

In the example above, you can see the serial number of the motherboard but not the system. For that you can use dmidecode -t 1. It can be a hassle to parse all that so just pipe it through sha256sum and get a simple hash.

Using dmidecode -s system-uuid could be useful as well.

Post Status

Asked in February 2016
Viewed 1,953 times
Voted 6
Answered 1 times

Search




Leave an answer