Last active
October 13, 2015 13:07
-
-
Save itsjohncs/4200127 to your computer and use it in GitHub Desktop.
galah.sisyphus init script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This is free and unencumbered software released into the public domain. | |
# | |
# Anyone is free to copy, modify, publish, use, compile, sell, or | |
# distribute this software, either in source code form or as a compiled | |
# binary, for any purpose, commercial or non-commercial, and by any | |
# means. | |
# | |
# In jurisdictions that recognize copyright laws, the author or authors | |
# of this software dedicate any and all copyright interest in the | |
# software to the public domain. We make this dedication for the benefit | |
# of the public at large and to the detriment of our heirs and | |
# successors. We intend this dedication to be an overt act of | |
# relinquishment in perpetuity of all present and future rights to this | |
# software under copyright law. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
# OTHER DEALINGS IN THE SOFTWARE. | |
# | |
# For more information, please refer to <http://unlicense.org/> | |
### BEGIN INIT INFO | |
# Provides: galah-sisyphus | |
# Required-Start: $remote_fs $syslog $networking $mongod | |
# Required-Stop: $remote_fs $syslog $networking $mongod | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Daemon for the galah-sisyphus. | |
# Description: Controls Galah's sisyphus component. | |
### END INIT INFO | |
# Get lots of meta data about where we're running from so we can do the evil | |
# recurssion. | |
DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
ME=`basename $0` | |
ME_FULL=$DIR/$ME | |
# The user that sisyphus will be ran under | |
SISYPHUS_USER="www-data" | |
# The location of the uwsgi process to use | |
SISYPHUS_PATH="/home/www-data/galah/galah/sisyphus/sisyphus.py" | |
# The location of the python interpretor to use (should be the interpreter | |
# inside of the virtual environment's bin directory). | |
PYTHON="/home/www-data/env/bin/python" | |
NAME="galah-sisyphus" | |
PIDFILE="/var/run/$NAME.pid" | |
BOLD=`tput bold` | |
RED_BOLD=$BOLD`tput setaf 1` | |
GREEN_BOLD=$BOLD`tput setaf 2` | |
RESET=`tput sgr0` | |
case "$1" in | |
start) | |
# If you are not root below, then su will request a password which could | |
# cause problems since init scripts are usually assumend to not be | |
# interactive. | |
if [[ $EUID -ne 0 ]]; then | |
echo "${RED_BOLD}Must be root.$RESET" | |
exit 4 | |
fi | |
$ME_FULL status > /dev/null 2>&1 | |
CURRENT_STATUS=$? | |
if [[ $CURRENT_STATUS != "3" ]]; then | |
if [[ $CURRENT_STATUS == "1" ]]; then | |
echo "Starting $NAME: [${RED_BOLD}FAIL$RESET]" | |
echo "${RED_BOLD}$NAME not running but pidfile exists at $PIDFILE. The pidfile must be removed before restarting Galah.$RESET" | |
exit 1 | |
fi | |
echo "Starting $NAME: [${RED_BOLD}ALREADY RUNNING$RESET]" | |
exit 1 | |
fi | |
echo -n "Starting $NAME: [" | |
PID=$(su -c "$PYTHON $SISYPHUS_PATH > /dev/null 2>&1 & echo \$!" $SISYPHUS_USER) | |
# Give the job a moment to fail. | |
sleep 1 | |
RUNNING=`kill -0 $PID 2> /dev/null; echo $?` | |
if [[ -z $PID || $RUNNING != "0" ]]; then | |
echo "${RED_BOLD}FAIL${RESET}]" | |
exit 1 | |
else | |
echo $PID > $PIDFILE | |
echo "${GREEN_BOLD}DONE${RESET}]" | |
exit 0 | |
fi | |
;; | |
status) | |
if [ -f $PIDFILE ]; then | |
PID=`cat $PIDFILE` | |
if kill -0 $PID > /dev/null 2>&1; then | |
echo "$NAME running, PID $PID." | |
exit 0 | |
else | |
echo "${RED_BOLD}$NAME not running but pidfile exists at $PIDFILE.$RESET" | |
exit 1 | |
fi | |
else | |
echo "$NAME not running." | |
exit 3 | |
fi | |
;; | |
stop) | |
echo -n "Stopping $NAME: [" | |
if [ -f $PIDFILE ]; then | |
PID=`cat $PIDFILE` | |
KILL_OUTPUT=$(kill -TERM $PID 2>&1) | |
if [[ $? == "0" ]]; then | |
sleep 2 | |
$ME_FULL status > /dev/null 2>&1 == "3" | |
if [[ $? == "3" || $? == "1" ]]; then | |
rm -f $PIDFILE | |
echo "${GREEN_BOLD}DONE$RESET]" | |
exit 0 | |
else | |
echo "${RED_BOLD}FAIL$RESET]" | |
echo "${RED}$NAME ($PID) did not stop in time." | |
exit 0 | |
fi | |
else | |
echo "${RED_BOLD}FAIL$RESET]" | |
echo $RED$KILL_OUTPUT$RESET | |
exit 1 | |
fi | |
else | |
echo "${RED_BOLD}NOT RUNNING$RESET]" | |
exit 0 | |
fi | |
;; | |
force-reload) | |
# Recurse and call restart | |
$ME_FULL restart | |
exit $? | |
;; | |
restart) | |
# Recurse and call stop then start. | |
if $ME_FULL stop; then | |
$ME_FULL start | |
exit $? | |
fi | |
exit $? | |
;; | |
reload) | |
echo "${RED_BOLD}RELOAD NOT IMPLEMENTED. USE RESTART.$RESET" | |
exit 3 | |
;; | |
where-pid) | |
echo $PIDFILE | |
;; | |
*) | |
echo "Usage: $0 {status|start|stop|restart|force-reload|where-pid}" | |
exit 2 | |
esac | |
# A user should not get to this point. | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment