-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_run_id.sh
68 lines (57 loc) · 1.9 KB
/
get_run_id.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Bash script to assign a run id/number
# -------------------------------------
# Usage:
# ./get_run_id.sh [prefix]
#
# If network drive is found, in other words if we're on the
# local network, use the global run numbering. Otherwise
# use numbering local to this machine and prepend the
# machine name so we know the difference.
# Network location of run number counters
RUN_NUMBER_DIR=/r/Urbansim2/run-numbers
# Should be no need to change anything below this line.
# -----------------------------------------------------
# Use a run prefix if user supplied one, e.g. luv -> luv-0001
if [ -n "$1" ]
then
PREFIX="$1"
else
PREFIX="run"
fi
# If network location is not found, prepend hostname and use ~/.run-numbers
if [ ! -d $RUN_NUMBER_DIR ]
then
mkdir -p ~/.run-numbers
RUN_NUMBER_DIR=~/.run-numbers
PREFIX=$(hostname)-$PREFIX
fi
cd $RUN_NUMBER_DIR
run_number=$PREFIX-number.txt
# Attempt to get a run number five times
for loop in 1 2 3 4 5
do
# use a lockfile to prevent script from running from multiple threads
# (if something goes wrong, just delete $RUN_NUMBER_DIR/lockfile)
if ( set -o noclobber; echo "$$" > lockfile-delete-me.txt) 2> /dev/null; then
trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
# read last run number from file, if it exists
if [ -e $run_number ]
then
OLD_RUN=`cat "$run_number"`
fi
# calculate and save new run number
NEW_RUN=$((OLD_RUN + 1))
echo $NEW_RUN > "$run_number"
printf "%s-%04d\n" "$PREFIX" "$NEW_RUN"
# clean up after yourself, and release your lock trap
rm -f lockfile-delete-me.txt
trap - INT TERM EXIT
exit 0
else
# lock exists, wait and try again in a second
(>&2 echo Script is locked, trying again...)
sleep 2
fi
done
(>&2 echo "Lockfile $RUN_NUMBER_DIR/$lockfile exists, aborting")
exit 2