Skip to content

Instantly share code, notes, and snippets.

@ezmobius
Created August 1, 2008 01:42
Show Gist options
  • Save ezmobius/3557 to your computer and use it in GitHub Desktop.
Save ezmobius/3557 to your computer and use it in GitHub Desktop.
#!/bin/sh
# This script should live in /engineyard/bin/ and be named monit_merb
mdk_merb() {
if [ -e "${PIDFILE}" ]; then
OLDPID=`cat ${PIDFILE}`
if [ ! -d /proc/$OLDPID ]; then
rm -f $PIDFILE
OLDPID=""
fi
fi
#Pid file didn't exist, ask pgrep for it
if [ "$OLDPID" = "" ]; then
OLDPID=`pgrep -f "ruby.*merb -p $PORT"`
fi
if [ "$OLDPID" != "" ]; then
#Try stopping it gracefully, kill $OLDPID is the same as cluster::stop
kill $OLDPID
# 5 seconds really isn't enough of a wait time, but it's good for situations where shit hit the fan
sleep 5
#Does the process still exist? If it does, client sites are gonna white screen
if [ -e /proc/$OLDPID ]; then
#Murder the damn thing. Kill em, fuckin' kill em.
kill -9 $OLDPID
sleep 3
#Does it still exist!?! Atleast give some log love alerting us that it failed
if [ -e /proc/$OLDPID ]; then
#Alert someone? and exit with an error
echo "Process $OLDPID won't die!" >> /var/log/engineyard/merb/$APPLICATION/merb.$PORT.log 2>&1
exit 1
fi
fi
fi
[ -e "$PIDFILE" ] && rm -f $PIDFILE
return 0
}
usage() {
echo "Usage: $0 application {start, stop} port"
echo "$0 kongregate start 5000"
echo "You can also run it w/o parameters from monit"
echo "and the application/command/port will be inferred by monit env variables"
echo "These variables are MONIT_SERVICE and MONIT_EVENT"
echo "$0"
exit 1
}
##
# script actually starts
##
if [ $# -lt 3 ]; then # running under monit w/o params
ACTION="`echo "$MONIT_EVENT" | tr \"[:upper:]\" \"[:lower:]\"`" # translate Start to start and Stop to stop
APPLICATION="`echo $MONIT_SERVICE | perl -p -e 's/^([^_]+)_(.*)?_(\d+)/$2/'`" # grab the stuff between the services
PORT="`echo $MONIT_SERVICE | perl -p -e 's/^([^_]+)_(.*)?_(\d+)/$3/'`" # grab the port out of the last stuff
ENVIRONMENT="production"
echo "Grabbed params from monit $APPLICATION,$ACTION,$PORT" > /var/log/engineyard/merb/monit_merb.log 2>&1
# [ -z $ACTION ] || [ -z $APPLICATION ] || [ -z $PORT ] && usage
elif [ $# -lt 4 ]; then
APPLICATION=$1 ; ACTION=$2 ; PORT=$3 ; USER=$1 ; ENVIRONMENT="production"
else
APPLICATION=$1 ; ACTION=$2 ; PORT=$3 ; USER=$4 ; ENVIRONMENT=$5
fi
# Now that we have some variables let's set the PIDFILE
PIDFILE="/data/$APPLICATION/shared/log/merb.$PORT.pid"
INLINEDIR=/tmp/inline_$$; export INLINEDIR
HOME=$USER; export HOME
export USER
# what it do?
case $ACTION in
start*)
mdk_merb
cd /data/$APPLICATION/current
MERB="/usr/bin/merb -p $PORT -e $ENVIRONMENT -d -u $USER -G $USER"
if [ `merb -v | sed 's/^.*merb [0-9].\([0-9]\).[0-9].*$/\1/'` -ge 9 ]; then
PIDFILE="/var/run/merb/$APPLICATION/merb.$PORT.pid"
MERB="$MERB -P $PIDFILE -e $ENVIRONMENT -L /var/log/engineyard/merb/$APPLICATION/merb.$PORT.log -m /data/$APPLICATION/current"
fi
echo $MERB
`$MERB`
STATUS=$?
if [ "$STATUS" = "0" -a -x /engineyard/bin/eyadjust ]; then
sleep 2
/engineyard/bin/eyadjust
fi
exit $STATUS
;;
stop*)
mdk_merb
exit $?
;;
*)
echo 2>&1 'Unrecognized Monit Command: $ACTION'
usage
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment