]> arthur.barton.de Git - trigger-rcmd.git/blobdiff - scripts/trigger-rcmd
Don't indicate error for "no wake" systems
[trigger-rcmd.git] / scripts / trigger-rcmd
index 75aeddb8ac0acbefbd5a4332f5dcd2b8fe4c860d..ec70d958f2864ae3c42912115fbbccb615d3bb4f 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/sh
 #
 # trigger-rcmp: Trigger remote commands
-# Copyright (c)2014-2016 Alexander Barton (alex@barton.de)
+# Copyright (c)2014-2017 Alexander Barton (alex@barton.de)
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -10,7 +10,8 @@
 # Please read the file COPYING, README and AUTHORS for more information.
 #
 
-NAME=$(basename "$0")
+BASENAME=$(basename "$0")
+NAME="$BASENAME"
 RCMD_D="/usr/local/etc/rcmd.d"
 
 PATH="$PATH:/usr/local/sbin:/usr/sbin"
@@ -39,8 +40,6 @@ do_rcmd() {
                return 2
        fi
 
-       NAME="$NAME($1)"
-
        echo "$NAME: Checking system \"$SYSTEM\" ..."
        fping -c1 -q "$SYSTEM" 2>/dev/null
        if [ $? -ne 0 ]; then
@@ -74,12 +73,14 @@ do_rcmd() {
        fi
 
        for i in $(seq 1 $SSH_CHECKS); do
-               echo "$NAME: Checking SSH connection ($i/$SSH_CHECKS) ..."
-               ssh -q "$SYSTEM" true >/dev/null 2>&1 && break
+               echo "$NAME: Checking SSH connection [publickey] ($i/$SSH_CHECKS) ..."
+               if ssh -o PreferredAuthentications=publickey -q "$SYSTEM" true >/dev/null 2>&1; then
+                       echo "$NAME: Ok, SSH seems to be available on \"$SYSTEM\"."
+                       break
+               fi
                echo "$NAME: SSH on \"$SYSTEM\" not ready. Waiting ..."
                sleep 10s
        done
-       echo "$NAME: Ok, SSH seems to be available on \"$SYSTEM\"."
 
        if [ "$rsys_was_running" -eq 0 ]; then
                echo "$NAME: \"$SYSTEM\" just started up. Delaying actions for $BOOT_DELAY ..."
@@ -90,7 +91,7 @@ do_rcmd() {
        echo
 
        # shellcheck disable=2029
-       ssh -q "$SYSTEM" "$CMD" </dev/null ; r=$?
+       ssh -o PreferredAuthentications=publickey -q "$SYSTEM" "$CMD" </dev/null ; r=$?
 
        echo
        echo "$NAME: Remote command ended with return code $r."
@@ -101,7 +102,7 @@ do_rcmd() {
                if [ "$rsys_users" = "0" ]; then
                        echo "$NAME: Power off \"$SYSTEM\" again ..."
                        # shellcheck disable=2029
-                       ssh -q "$SYSTEM" "sync; shutdown -hP $SHUTDOWN_TIME" >/dev/null
+                       ssh -o PreferredAuthentications=publickey -q "$SYSTEM" "sync; shutdown -hP $SHUTDOWN_TIME" >/dev/null
                        if [ $? -ne 0 ]; then
                                echo "$NAME: Failed to power off \"$SYSTEM\"!" >&2
                                return 5
@@ -122,7 +123,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
@@ -151,10 +152,24 @@ 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")"
+               NAME="$BASENAME($1)"
+               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
+               NAME=$BASENAME
+       fi
+       if [ $r -ne 0 ]; then
+               # Exit code indicates an error. But only flag this as error
+               # when target should have been woken up and the indicated error
+               # is not "system down" (9).
+               [ -z "$NO_WAKE" ] || [ $r -ne 9 ] && result=1
        fi
-       [ $r -ne 0 ] && result=1
        shift
        [ $# -gt 0 ] && echo
 done