Skip to content

Instantly share code, notes, and snippets.

@nickpack
Created September 16, 2013 22:34
Show Gist options
  • Save nickpack/6587510 to your computer and use it in GitHub Desktop.
Save nickpack/6587510 to your computer and use it in GitHub Desktop.
Some opencms service management nonsense I had to write for an interview 'aptitude' test.
#!/bin/bash
declare -a OPENCMS_SERVICES=('mysqld' 'tomcat6' 'nginx');
check_health() {
for service in ${OPENCMS_SERVICES[@]}
do
if [ -z "$(ps ax | grep -v grep | grep $service)" ]
then
echo "$service is NOT RUNNING..."
make_service_run $service
if [ -z "$(ps ax | grep -v grep | grep $service)" ]
then
echo "Unable to start $service."
fi
else
echo "$service is running..."
fi
done
}
make_service_run() {
if [ -z "$(ps ax | grep -v grep | grep $service)" ]
then
service $1 start
else
service $1 restart
fi
}
make_service_stop() {
if [ -z "$(ps ax | grep -v grep | grep $service)" ]
then
echo "$service isn't running!"
else
service $1 stop
fi
}
restart_all() {
for service in ${OPENCMS_SERVICES[@]}
do
make_service_run $service
done
}
stop_all() {
for service in ${OPENCMS_SERVICES[@]}
do
make_service_stop $service
done
}
if [ $# -lt 1 ]
then
echo "Usage : $0 [action]"
exit
fi
case "$1" in
stop) echo "Stopping..."
stop_all
;;
restart) echo "Restarting..."
restart_all
;;
start) echo "Starting..."
restart_all
;;
health) echo "Checking service health..."
check_health
;;
*) echo "Unknown action $1"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment