Skip to content

Instantly share code, notes, and snippets.

@Xowap
Created July 10, 2017 14:58
Show Gist options
  • Save Xowap/efe859fc9633ec833068b6f9575b0a9d to your computer and use it in GitHub Desktop.
Save Xowap/efe859fc9633ec833068b6f9575b0a9d to your computer and use it in GitHub Desktop.
#!/bin/bash
SERVBIN=${SERVBIN:-/usr/sbin/service}
success=0
function echo_rep {
echo -n $(tput cuu1)
echo -n $(tput ed)
echo "$1"
}
function echo_status {
service="$1"
status="$2"
prelude=" * $service: "
case "$status" in
dead)
echo_rep "$prelude$(tput setaf 1)dead$(tput sgr0)"
;;
restarting)
echo_rep "$prelude$(tput setaf 6)restarting$(tput sgr0)"
;;
started)
echo_rep "$prelude$(tput setaf 2)started$(tput sgr0)"
;;
failed)
echo_rep "$prelude$(tput setaf 1)$(tput rev)failed$(tput sgr0)"
;;
esac
}
function silent {
$@ &> /dev/null
return $?
}
function ensure_started {
service="$1"
echo
if silent $SERVBIN $service status
then
echo_status $service started
else
echo_status $service dead
echo_status $service restarting
if silent $SERVBIN $service restart
then
echo_status $service started
else
echo_status $service failed
success=1
fi
fi
}
if [ $# -eq 0 ]
then
echo "$(tput setaf 5)$0 -- $(tput bold)Service status checking script$(tput sgr0)"
echo "$(tput rev)usage:$(tput sgr0) supply a list of services as arguments and this" \
"script will check that those services are still running."\
"If they are not, an attempt to start them will be done."
echo "It uses the $(tput setaf 6)service$(tput sgr0) utility."\
"By default it goes to /usr/sbin/service, but you can"\
"set its value using the $(tput setaf 6)SERVBIN$(tput sgr0)"\
"environment variable."
exit 1
fi
echo "$(tput setaf 5)[moshi moshi]$(tput bold) Checking services...$(tput sgr0)"
echo
for service in "$@"
do
ensure_started $service
done
echo
if [ $success -eq 0 ]
then
echo "$(tput setaf 2)$(tput bold)All good!$(tput sgr0)"
else
echo "$(tput setaf 1)$(tput bold)Failure! Good luck.$(tput sgr0)"
fi
exit $success
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment