Skip to content

Instantly share code, notes, and snippets.

@Decstasy
Created December 23, 2017 17:05
Show Gist options
  • Save Decstasy/a0493258fd40176935cd6b173526194c to your computer and use it in GitHub Desktop.
Save Decstasy/a0493258fd40176935cd6b173526194c to your computer and use it in GitHub Desktop.
#!/bin/bash
# Dennis U.
# request@decstasy.de
#
# Script to shutdown local machine when a remote host is not available
# Check out the exception_present function!
#
# V 0.1 Initial release
# V 0.2 Added support for exceptions
### for debugging
if [[ "$1" = "--debug" ]]; then
shift
set -x
fi
### Vars
PATH=$PATH:/sbin
pidfile="/var/run/$(basename ${0}).pid"
declare -A shutdown
shutdown[delay]="5"
shutdown[initiate]="shutdown +${shutdown[delay]} 'Automatic shutdown triggered by $(basename ${0}) script'"
shutdown[abort]="shutdown -c 'Automatic shutdown aborted by $(basename ${0}) script'"
### Functions
manual() {
cat <<EOF
Usage: $0 [IP ADDRESS]
This script will poweroff your machine when the remote host is not reachable for more than ${shutdown[delay]} minutes.
You can alter the delay and commands in the beginning of the script. For debugging use --debug as your first argument.
EOF
_exit 0
}
_exit() {
# $1 exit code
# $2 optional output message
if [[ ! -z "$2" ]]; then
echo "$2" | logger -t check_host_shutdown
fi
rm $pidfile
exit $1
}
exception_present() {
# Return 0 exception found
# Return 1 no exception present and go on
### No shutdown if we have established samba sessions
#[[ $( lsof -iTCP -a -c smbd | grep -c 'ESTABLISHED' ) -ge 1 ]] && return 0
### No shutdown if Server has rsync processes
#ps -C rsync >/dev/null 2>&1 && return 0
### Continue to host check
return 1
}
initiate_shutdown() {
eval ${shutdown[initiate]}
}
abort_shutdown() {
eval ${shutdown[abort]}
_exit 0 "Automatic shutdown aborted. The remote host is reachable again."
}
is_alive() {
ping -nqc 1 -W 1 $1 >/dev/null 2>&1
}
check_host() {
is_alive $1 && _exit 0 || initiate_shutdown
echo "$1 is not alive. Shutdown in ${shutdown[delay]} minutes if it's not back online." | logger -t check_host_shutdown
# Wait loop
while true; do
sleep 5
is_alive $1 && abort_shutdown
done
}
#########################################################
################## MAIN ###############################
#####################################################
### Check for execution (we dont want messy effects with multiple executions)
if [[ -r "$pidfile" ]]; then
if kill -0 &>1 > /dev/null $(<"$pidfile"); then
# Script is already running
exit 0
else
rm "$pidfile"
fi
fi
echo $$ > "$pidfile"
### Trap if script is aborted
trap abort_shutdown INT
### Check Args
[[ $# -ne 1 ]] && manual
### Check for valid ip
if ! grep -P '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$' <<<$1 >/dev/null 2>&1; then
_exit 1 "Your IP is not valid!"
fi
### Check for exceptions
if exception_present; then
_exit 0
fi
### Action :)
check_host $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment