Skip to content

Instantly share code, notes, and snippets.

@mryoshio
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mryoshio/4cd42a34040d94021f6a to your computer and use it in GitHub Desktop.
Save mryoshio/4cd42a34040d94021f6a to your computer and use it in GitHub Desktop.
sidekiq init script sample
#!/bin/sh
#
# sidekiq_tkikaku Init script for Sidekiq tkikaku
#
# chkconfig: 345 75 25
# description: Starts and Stops Sidekiq message processor for Stratus application.
# processname: sidekiq_tkikaku
# user-specified exit parameters used in this script:
#
# Exit Code 5 - Incorrect User ID
# Exit Code 6 - Directory not found
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0
# Variables
APP="tkikaku"
RAILS_ENV="staging"
SERVICE_NAME="sidekiq_${APP}_${RAILS_ENV}"
APP_DIR="/var/www/html/${APP}/current"
APP_CONFIG="${APP_DIR}/config/sidekiq.yml"
LOCK_FILE="${APP_DIR}/tmp/pids/sidekiq.lock"
PID_FILE="${APP_DIR}/tmp/pids/sidekiq.pid"
RUNAS=ec2-user
CMD="cd ${APP_DIR} && bundle exec sidekiq -e ${RAILS_ENV} -C ${APP_CONFIG} --daemon"
RETVAL=0
start() {
echo -n "Starting ${SERVICE_NAME}: "
status
if [ $? -eq 1 ]; then
[ `id -u` == '0' ] || (echo "sidekiq runs as root only .."; exit 5)
[ -d $APP_DIR ] || (echo "$APP_DIR not found!.. Exiting"; exit 6)
su -c "$CMD" - $RUNAS
RETVAL=$?
# sleeping for 8 seconds for process to be precisely visible in process table
sleep 8
echo_success
echo
[ $RETVAL -eq 0 ] && su -c "touch ${LOCK_FILE}" - $RUNAS
return $RETVAL
else
echo_failure
echo "already running"
fi
}
stop() {
echo -n "Stopping ${SERVICE_NAME}: "
killproc -p $PID_FILE $APP -TERM
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
return $RETVAL
}
restart() {
stop
# avoid race
sleep 3
start
}
status() {
ps -ef | egrep "sidekiq [0-9]+.[0-9]+.[0-9]+ ${APP} \[" | grep -v grep
return $?
}
usage() {
echo "Usage: $0 {start|stop|restart|status}"
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
if [ $? -eq 0 ]; then
echo "sidekiq is running .."
RETVAL=0
else
echo "sidekiq is stopped .."
RETVAL=1
fi
;;
*)
usage
exit 0
;;
esac
exit $RETVAL
le
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment