3 # trigger-rcmp: Trigger remote commands
4 # Copyright (c)2014-2016 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.
14 RCMD_D="/usr/local/etc/rcmd.d"
16 PATH="$PATH:/usr/local/sbin:/usr/sbin"
19 echo "Usage: $0 [--no-wake|-n] <rcmd> [<rcmd> [...]]" >&2
33 # Read in job/configuration file, but ignore shellcheck(1) warnings:
34 # shellcheck disable=1090
38 echo "$NAME: Failed to read \"$cnf\"!"
44 echo "$NAME: Checking system \"$SYSTEM\" ..."
45 fping -c1 -q "$SYSTEM" 2>/dev/null
47 if [ -n "$NO_WAKE" ]; then
48 echo "$NAME: \"$SYSTEM\" seems to be down, skipping job."
52 echo "$NAME: \"$SYSTEM\" seems to be down, wake it up ..."
53 etherwake -b -i "$IFACE" "$(echo "$SYSTEM" | cut -d'.' -f1)"
55 echo "$NAME: Failed to wake \"$SYSTEM\"!" >&2
58 echo "$NAME: Waiting for \"$SYSTEM\" to respond ..."
59 for i in $(seq 1 $TIMEOUT); do
60 fping -c1 -q "$SYSTEM" 2>/dev/null
64 fping -c1 -q "$SYSTEM" 2>/dev/null
66 echo "$NAME: Failed to wake \"$SYSTEM\"!" >&2
72 echo "$NAME: \"$SYSTEM\" is already alive, ok."
76 for i in $(seq 1 $SSH_CHECKS); do
77 echo "$NAME: Checking SSH connection ($i/$SSH_CHECKS) ..."
78 ssh -q "$SYSTEM" true >/dev/null 2>&1 && break
79 echo "$NAME: SSH on \"$SYSTEM\" not ready. Waiting ..."
82 echo "$NAME: Ok, SSH seems to be available on \"$SYSTEM\"."
84 if [ "$rsys_was_running" -eq 0 ]; then
85 echo "$NAME: \"$SYSTEM\" just started up. Delaying actions for $BOOT_DELAY ..."
89 echo "$NAME: Calling \"$CMD\" on \"$SYSTEM\" ..."
92 # shellcheck disable=2029
93 ssh -q "$SYSTEM" "$CMD" </dev/null ; r=$?
96 echo "$NAME: Remote command ended with return code $r."
99 if [ "$rsys_was_running" -eq 0 ]; then
100 rsys_users=$(ssh -q "$SYSTEM" w | tail -n +3 | wc -l)
101 if [ "$rsys_users" = "0" ]; then
102 echo "$NAME: Power off \"$SYSTEM\" again ..."
103 # shellcheck disable=2029
104 ssh -q "$SYSTEM" "sync; shutdown -hP $SHUTDOWN_TIME" >/dev/null
105 if [ $? -ne 0 ]; then
106 echo "$NAME: Failed to power off \"$SYSTEM\"!" >&2
110 echo "$NAME: Not shutting down \"$SYSTEM\", \c"
111 [ "$rsys_users" -eq 1 ] \
112 && echo "There is 1 user logged in!" \
113 || echo "There are $rsys_users users logged in!"
116 echo "$NAME: \"$SYSTEM\" was already alive, not shutting down."
118 [ $r -eq 0 ] && return 0 || return 1
124 # Check required tools
125 for t in fping ssh etherwake; do
126 if ! command -v "$t" >/dev/null 2>&1; then
127 echo "$NAME: Required tool \"$t\" missing, aborting!"
132 while [ $# -gt 0 ]; do
144 [ $# -gt 0 ] || do_help
147 while [ $# -gt 0 ]; do
149 if [ ! -r "$cnf" ]; then
150 echo "$NAME: Can't read \"$cnf\"!"
153 echo "$NAME: Working on \"$1\" - $(date "+%Y-%m-%d %H:%M:%S")"
155 echo "$NAME: Done ($r) - $(date "+%Y-%m-%d %H:%M:%S")"
157 [ $r -ne 0 ] && result=1