Skip to content
Snippets Groups Projects

Lock

Merged Alex Iribarren requested to merge lock into master
2 files
+ 24
1
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 23
0
#!/bin/bash
OUTPUT='/alloc/output.json'
LOCKFILE='/repo/.rsynclock'
log () {
mapfile IN
@@ -19,6 +20,24 @@ error () {
EOF
}
# Let's make absolutely sure there's only one copy of the job running
if [[ -f "$LOCKFILE" ]]; then
# The lockfile exists, let's check if it's fresh
AGE=$((`date +%s` - `stat -c '%Y' $LOCKFILE`))
if [[ $AGE -gt 86400 ]]; then
echo "$LOCKFILE is over 24 hours old. It's probably stale, so get rid of it."
rm -f "$LOCKFILE"
fi
fi
# Grab the lock, exit if you fail
if !(set -o noclobber; echo "`hostname` ($$)" > "$LOCKFILE") 2> /dev/null; then
echo "Lock exists: $LOCKFILE owned by $(cat $LOCKFILE)"
exit
fi
# Delete the lock if we die
trap 'rm -f "$LOCKFILE"; exit $?' INT TERM EXIT
# Read the job parameters
if [[ "${TOOL}" == "rsync" ]]; then
@@ -58,3 +77,7 @@ cat << EOF | log > $OUTPUT
"message_type": "result",
"exit_code": ${RET}
EOF
# Clean up after yourself, and release your trap
rm -f "$LOCKFILE"
trap - INT TERM EXIT
Loading