3 # trigger-rcmp: Trigger remote commands
4 # Copyright (c)2014 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 NAME="`basename "$0"`"
14 RCMD_D="/usr/local/etc/rcmd.d"
16 PATH="$PATH:/usr/local/sbin:/usr/sbin"
19 echo "Usage: $0 <rcmd> [<rcmd> [...]]" >&2
36 echo "$NAME: failed to read \"$cnf\"!"
42 echo "$NAME: checking system \"$SYSTEM\" ..."
43 fping -c1 -q "$SYSTEM" 2>/dev/null
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 etherwake -b -i "$IFACE" `echo "$SYSTEM" | cut -d'.' -f1`
53 echo "$NAME: failed to wake \"$SYSTEM\"!" >&2
56 echo "$NAME: waiting for \"$SYSTEM\" to respond ..."
57 for i in `seq 1 $TIMEOUT`; do
58 fping -c1 -q "$SYSTEM" 2>/dev/null
62 fping -c1 -q "$SYSTEM" 2>/dev/null
64 echo "$NAME: failed to wake \"$SYSTEM\"!" >&2
70 echo "$NAME: \"$SYSTEM\" is already alive, ok."
74 for i in `seq 1 $SSH_CHECKS`; do
75 echo "$NAME: checking SSH connection ($i/$SSH_CHECKS) ..."
76 ssh -q "$SYSTEM" true >/dev/null 2>&1 && break
77 echo "$NAME: SSH on \"$SYSTEM\" not ready. Waiting ..."
80 echo "$NAME: ok, SSH seems to be available on \"$SYSTEM\"."
82 if [ "$rsys_was_running" -eq 0 ]; then
83 echo "$NAME: \"$SYSTEM\" just started up. Delaying actions for $BOOT_DELAY ..."
87 echo "$NAME: calling \"$CMD\" on \"$SYSTEM\" ..."
90 ssh -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 ssh -q "$SYSTEM" "sync; shutdown -hP $SHUTDOWN_TIME" >/dev/null
101 if [ $? -ne 0 ]; 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 while [ $# -gt 0 ]; do
132 [ $# -gt 0 ] || do_help
135 while [ $# -gt 0 ]; do
137 if [ ! -r "$cnf" ]; then
138 echo "$NAME: can't read \"$cnf\"!"
141 echo "$NAME: working on \"$1\" - `date "+%Y-%m-%d %H:%M:%S"`"
143 echo "$NAME: done ($r) - `date "+%Y-%m-%d %H:%M:%S"`"
145 [ $r -ne 0 ] && result=1