3 # trigger-rcmp: Trigger remote commands
4 # Copyright (c)2014-2020 Alexander Barton (alex@barton.de)
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 # Please read the file COPYING, README and AUTHORS for more information.
13 BASENAME=$(basename "$0")
15 RCMD_D="/usr/local/etc/rcmd.d"
17 PATH="$PATH:/usr/local/sbin:/usr/sbin"
20 echo "Usage: $0 [--no-wake|-n] <rcmd> [<rcmd> [...]]" >&2
34 # Read in job/configuration file, but ignore shellcheck(1) warnings:
35 # shellcheck disable=1090
39 echo "$NAME: Failed to read \"$cnf\"!"
43 echo "$NAME: Checking system \"$SYSTEM\" ..."
44 if ! fping -c1 -q "$SYSTEM" 2>/dev/null; then
45 if [ -n "$NO_WAKE" ]; then
46 echo "$NAME: \"$SYSTEM\" seems to be down, skipping job."
50 echo "$NAME: \"$SYSTEM\" seems to be down, wake it up ..."
51 if ! etherwake -b -i "$IFACE" "$(echo "$SYSTEM" | cut -d'.' -f1)"; then
52 echo "$NAME: Failed to wake \"$SYSTEM\"!" >&2
55 echo "$NAME: Waiting for \"$SYSTEM\" to respond ..."
56 for i in $(seq 1 $TIMEOUT); do
57 fping -c1 -q "$SYSTEM" 2>/dev/null && break
60 if ! fping -c1 -q "$SYSTEM" 2>/dev/null; then
61 echo "$NAME: Failed to wake \"$SYSTEM\"!" >&2
67 echo "$NAME: \"$SYSTEM\" is already alive, ok."
71 for i in $(seq 1 $SSH_CHECKS); do
72 echo "$NAME: Checking SSH connection [publickey] ($i/$SSH_CHECKS) ..."
73 if ssh -o PreferredAuthentications=publickey -q "$SYSTEM" true >/dev/null 2>&1; then
74 echo "$NAME: Ok, SSH seems to be available on \"$SYSTEM\"."
77 echo "$NAME: SSH on \"$SYSTEM\" not ready. Waiting ..."
81 if [ "$rsys_was_running" -eq 0 ]; then
82 echo "$NAME: \"$SYSTEM\" just started up. Delaying actions for $BOOT_DELAY ..."
86 echo "$NAME: Calling \"$CMD\" on \"$SYSTEM\" ..."
89 # shellcheck disable=2029
90 ssh -o PreferredAuthentications=publickey -q "$SYSTEM" "$CMD" </dev/null ; r=$?
93 echo "$NAME: Remote command ended with return code $r."
96 if [ "$rsys_was_running" -eq 0 ]; then
97 rsys_users=$(ssh -q "$SYSTEM" w | tail -n +3 | wc -l)
98 if [ "$rsys_users" = "0" ]; then
99 echo "$NAME: Power off \"$SYSTEM\" again ..."
100 # shellcheck disable=2029
101 if ! ssh -o PreferredAuthentications=publickey -q "$SYSTEM" "sync; shutdown -hP $SHUTDOWN_TIME" >/dev/null 2>&1; then
102 echo "$NAME: Failed to power off \"$SYSTEM\"!" >&2
106 echo "$NAME: Not shutting down \"$SYSTEM\", \c"
107 [ "$rsys_users" -eq 1 ] \
108 && echo "There is 1 user logged in!" \
109 || echo "There are $rsys_users users logged in!"
112 echo "$NAME: \"$SYSTEM\" was already alive, not shutting down."
114 [ $r -eq 0 ] && return 0 || return 1
120 # Check required tools
121 for t in fping ssh etherwake lockfile-create; do
122 if ! command -v "$t" >/dev/null 2>&1; then
123 echo "$NAME: Required tool \"$t\" missing, aborting!"
128 while [ $# -gt 0 ]; do
140 [ $# -gt 0 ] || do_help
143 while [ $# -gt 0 ]; do
145 if [ ! -r "$cnf" ]; then
146 echo "$NAME: Can't read \"$cnf\"!"
149 echo "$NAME: Working on \"$1\" - $(date "+%Y-%m-%d %H:%M:%S")"
151 lck="/run/lock/$NAME-$1.lock"
152 if lockfile-create --quiet --use-pid --retry 2 --lock-name "$lck"; then
154 echo "$NAME: Done ($r) - $(date "+%Y-%m-%d %H:%M:%S")"
157 echo "$NAME: Failed to acquire lock file, skipping job! ($lck)"
162 if [ $r -ne 0 ]; then
163 # Exit code indicates an error. But only flag this as error
164 # when target should have been woken up and the indicated error
165 # is not "system down" (9).
166 [ -z "$NO_WAKE" ] || [ $r -ne 9 ] && result=1
171 echo "$NAME: All done, exit code $result - $(date "+%Y-%m-%d %H:%M:%S")"