]> arthur.barton.de Git - trigger-rcmd.git/commitdiff
Create and test for a lockfile for each job
authorAlexander Barton <alex@barton.de>
Tue, 20 Dec 2016 21:07:26 +0000 (22:07 +0100)
committerAlexander Barton <alex@barton.de>
Tue, 20 Dec 2016 21:07:26 +0000 (22:07 +0100)
scripts/trigger-rcmd

index 90c19bdb18107d7c505cecff9502b97c91f402db..cfd4d790df7c531e9fca9d6482f3cda984a9fa4e 100755 (executable)
@@ -124,7 +124,7 @@ do_rcmd() {
 NO_WAKE=
 
 # Check required tools
-for t in fping ssh etherwake; do
+for t in fping ssh etherwake lockfile-create; do
        if ! command -v "$t" >/dev/null 2>&1; then
                echo "$NAME: Required tool \"$t\" missing, aborting!"
                exit 3
@@ -153,8 +153,15 @@ while [ $# -gt 0 ]; do
                r=1
        else
                echo "$NAME: Working on \"$1\" - $(date "+%Y-%m-%d %H:%M:%S")"
-               do_rcmd "$1" ; r=$?
-               echo "$NAME: Done ($r) - $(date "+%Y-%m-%d %H:%M:%S")"
+               lck="/run/lock/$NAME-$1.lock"
+               if lockfile-create --quiet --use-pid --retry 2 --lock-name "$lck"; then
+                       do_rcmd "$1" ; r=$?
+                       echo "$NAME: Done ($r) - $(date "+%Y-%m-%d %H:%M:%S")"
+                       rm -f "$lck"
+               else
+                       echo "$NAME: Failed to acquire lock file, skipping job! ($lck)"
+                       r=1
+               fi
        fi
        [ $r -ne 0 ] && result=1
        shift