简体   繁体   中英

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}' 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, eg 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM