Skip to content

Instantly share code, notes, and snippets.

@merqlove
Last active August 29, 2015 14:22
Show Gist options
  • Save merqlove/cf8d4d4c3c722d25317a to your computer and use it in GitHub Desktop.
Save merqlove/cf8d4d4c3c722d25317a to your computer and use it in GitHub Desktop.
Sidekiq init script
#!/bin/bash
#
# Alexander Merkulov [email protected]
# Contributors : @merqlove
# App Version : 6.x
# chkconfig: 2345 82 55
# processname: sidekiq_some_service
# description: Runs sidekiq for some_service.
# Related:
# https://gist.github.com/merqlove/cf8d4d4c3c722d25317a
# https://gist.github.com/CD1212/5326706
# Save original $PATH
# /etc/rc.d/init.d/functions resets $PATH to default(/sbin:/usr/sbin:/bin:/usr/bin).
# Consequently, rvm and compiled ruby with custom path (which isn't /usr/bin) cannot be executed.
# Include RedHat function library
. /etc/rc.d/init.d/functions
# The name of the service
NAME="sidekiq_some_service"
# User name and paths
USER=someuser
APP_ROOT=/srv/u/some_service
APP_PATH="$APP_ROOT/current"
APP_SHARED="$APP_ROOT/shared"
CONFIG="$APP_SHARED/config"
# The PID and LOCK files used by sidekiq
UPID="$APP_SHARED/pids/sidekiq.pid"
ULOCK="/var/lock/subsys/$NAME"
OPTS="-C $APP_SHARED/config/sidekiq.yml -e production -d"
# Ruby related path update
RBENV_ROOT="/opt/rbenv"
RBENV_SHIMS="$RBENV_ROOT/shims"
RBENV_BIN="$RBENV_ROOT/bin"
RBENV_ROOT_PATCH="export RBENV_ROOT"
RUBY_PATH_PATCH="PATH=/usr/local/bin:/usr/local/lib:$RBENV_SHIMS:$RBENV_BIN:$PATH && export PATH"
start() {
cd $APP_PATH
# Start sidekiq
echo -n $"Starting sidekiq: "
daemon --pidfile=$UPID --user=$USER "$RUBY_PATH_PATCH && $RBENV_ROOT_PATCH && bundle exec sidekiq $OPTS"
sidekiq=$?
[ $sidekiq -eq 0 ] && touch $ULOCK
echo
return $sidekiq
}
stop() {
cd $APP_PATH
# Stop sidekiq
echo -n $"Stopping sidekiq: "
killproc -p $UPID
sidekiq=$?
[ $sidekiq -eq 0 ] && rm -f $ULOCK
echo
return $sidekiq
}
restart() {
stop
start
}
get_status() {
status -p $UPID $NAME
}
query_status() {
get_status >/dev/null 2>&1
}
case "$1" in
start)
query_status && exit 0
start
;;
stop)
query_status || exit 0
stop
;;
restart)
restart
;;
status)
get_status
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment