Skip to content

Instantly share code, notes, and snippets.

@tjl2
Last active December 14, 2015 00:09
Show Gist options
  • Save tjl2/4997227 to your computer and use it in GitHub Desktop.
Save tjl2/4997227 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# This script starts and stops the Resque scheduler plugin daemon
# This script belongs in /engineyard/bin/resque
#
PATH=/bin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
usage() {
echo "Usage: $0 <appname> <env> {start|stop}"
exit 1
}
if [ $# -lt 3 ]; then usage; fi
if [ "`whoami`" != "root" ]; then
logger -t `basename $0` -s "Must be run as root"
exit 1
fi
APP=$1
ENV=$2
ACTION=$3
# use rvm if application was configured as rvm alias
rvm_path=/usr/local/rvm
if [[ -s "${rvm_path}/environments/${APP}" ]]; then
PATH=${rvm_path}/wrappers/${$APP}:$PATH
fi
APP_DIR="/data/${APP}"
APP_ROOT="${APP_DIR}/current"
USER=`stat -c"%U" $APP_DIR`
HOME="/home/$USER" ; export HOME
GEMFILE="$APP_ROOT/Gemfile"
BUNDLER=""
if [ -f $GEMFILE ];then
BUNDLER="bundle exec"
fi
LOG_FILE="/var/log/engineyard/resque/$APP/resque-scheduler.log"
COMMAND="RAILS_ENV=$ENV $BUNDLER rake resque:scheduler --trace"
PID_FILE="/var/run/engineyard/resque/$APP/resque-scheduler.pid"
# handle the third param, don't start if already existing
case "$ACTION" in
start)
echo "Starting Resque Scheduler"
cd $APP_ROOT
if [ -f $PID_FILE ]; then
PID=`cat $PID_FILE`
if [ -d /proc/$PID ]; then
echo "Resque Scheduler is already running."
exit 1
fi
rm -f $PID_FILE
fi
touch $LOG_FILE
logger -t "monit-resque-scheduler[$$]" "Starting with: ${COMMAND}"
exec su -c"$COMMAND" $USER >> $LOG_FILE 2>&1 &
echo $! > $PID_FILE
;;
stop)
echo "Stopping Resque Scheduler"
if [ -f $PID_FILE ]; then
kill -15 `cat $PID_FILE` 2>/dev/null; true
fi
[ -e "$PID_FILE" ] && rm -f $PID_FILE
exit 0
;;
*)
usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment