Created
June 26, 2014 06:29
-
-
Save liuhui998/0f5319f0eb9017ebe2a2 to your computer and use it in GitHub Desktop.
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 | |
### BEGIN INIT INFO | |
# | |
# Provides : resque-env | |
# Required-Start : | |
# Required-Stop : | |
# Default-Start : 2 3 4 5 | |
# Default-Stop : 0 1 6 | |
# Short-Description : Resque worker via init.d (assumes you have Ruby, and a plugin to allow wildcards in queue names) | |
# Description : see Short-Description, brah | |
# | |
### END INIT INFO | |
PROG="rake" | |
NICE_NAME="resque" | |
PROG_DIR="/usr/local/bin" | |
USER="sardaukar" | |
PROG_PATH="/home/$USER/app/current" | |
PID_PATH="/var/run/" | |
RAILS_ENV="production" | |
capitalize_first () | |
{ | |
string0="$@" | |
firstchar=${string0:0:1} | |
string1=${string0:1} | |
FirstChar=`echo "$firstchar" | tr a-z A-Z` | |
echo "$FirstChar$string1" | |
} | |
toUpper() { | |
echo $1 | tr "[:lower:]" "[:upper:]" | |
} | |
RAILS_ENV_1ST=`capitalize_first $RAILS_ENV` | |
RAILS_ENV_HI=`toUpper $RAILS_ENV` | |
PROG_ARGS="environment resque:work QUEUE='*_$RAILS_ENV_HI'" | |
PID_FILE="$PID_PATH/$NICE_NAME-$RAILS_ENV_HI.pid" | |
start() { | |
if [ -e "$PID_FILE" ]; then | |
echo "Error! $RAILS_ENV_1ST Resque worker is currently running, or the pid file in $PID_PATH is stale! Sorry it didn't work out, brah." 1>&2 | |
exit 1 | |
else | |
su $USER -c "cd $PROG_PATH && $PROG_DIR/$PROG $PROG_ARGS RAILS_ENV=$RAILS_ENV 2>&1 >/dev/null &" & | |
echo "Hold on, brah - starting up that worker for you" | |
while [[ $WORKER_PID == "" ]] | |
do | |
WORKER_PID=`ps a | grep "Waiting for" | grep $RAILS_ENV_HI | ruby -e "puts STDIN.read.split(' ').first || ''"` | |
sleep 1 | |
echo -n "." | |
done | |
echo $WORKER_PID > "$PID_FILE" | |
echo "$RAILS_ENV_1ST Resque worker started, brah! PID is $WORKER_PID, btw." | |
fi | |
} | |
stop() { | |
if [ -e "$PID_FILE" ]; then | |
pid=`cat "$PID_FILE"` | |
kill $pid > /dev/null 2>&1 # not using -9 so as not to have output | |
sleep 7 # I should really check if the PID is still up here... | |
rm "$PID_FILE" | |
echo "$RAILS_ENV_HI Resque worker stopped, brah!" | |
else | |
echo "Error! $RAILS_ENV_1ST Resque worker is not running! C'mon, brah!" 1>&2 | |
exit 1 | |
fi | |
} | |
if [ "$(id -u)" != "0" ]; then | |
echo "This script must be run as root, brah!" 1>&2 | |
exit 1 | |
fi | |
case "$1" in | |
start) | |
start | |
exit 0 | |
;; | |
stop) | |
stop | |
exit 0 | |
;; | |
reload|restart|force-reload) | |
stop | |
start | |
exit 0 | |
;; | |
**) | |
echo "Yo, brah. Usage: $0 {start|stop|reload}" 1>&2 | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment