Skip to content

Instantly share code, notes, and snippets.

@dyerc
Last active Feb 27, 2020
Embed
What would you like to do?
Sidekiq init script
#!/bin/bash
# sidekiq Init script for Sidekiq
# chkconfig: 345 100 75
#
# Description: Starts and Stops Sidekiq message processor for Stratus application.
#
# User-specified exit parameters used in this script:
#
# Exit Code 5 - Incorrect User ID
# Exit Code 6 - Directory not found
# You will need to modify these
APP="myapp"
AS_USER="myuser"
APP_DIR="/var/www/${APP}/current"
APP_CONFIG="${APP_DIR}/config"
LOG_FILE="$APP_DIR/log/sidekiq.log"
LOCK_FILE="$APP_DIR/${APP}-lock"
PID_FILE="$APP_DIR/${APP}.pid"
GEMFILE="$APP_DIR/Gemfile"
SIDEKIQ="sidekiq"
APP_ENV="production"
BUNDLE="bundle"
START_CMD="$BUNDLE exec $SIDEKIQ -e $APP_ENV -P $PID_FILE"
CMD="cd ${APP_DIR}; ${START_CMD} >> ${LOG_FILE} 2>&1 &"
RETVAL=0
start() {
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)
cd $APP_DIR
echo "Starting $SIDEKIQ message processor .. "
su -c "$CMD" - $AS_USER
RETVAL=$?
#Sleeping for 8 seconds for process to be precisely visible in process table - See status ()
sleep 8
[ $RETVAL -eq 0 ] && touch $LOCK_FILE
return $RETVAL
else
echo "$SIDEKIQ message processor is already running .. "
fi
}
stop() {
echo "Stopping $SIDEKIQ message processor .."
SIG="INT"
kill -$SIG `cat $PID_FILE`
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
return $RETVAL
}
status() {
ps -ef | grep 'sidekiq [0-9].[0-9].[0-9]' | grep -v grep
return $?
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
if [ $? -eq 0 ]; then
echo "$SIDEKIQ message processor is running .."
RETVAL=0
else
echo "$SIDEKIQ message processor is stopped .."
RETVAL=1
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 0
;;
esac
exit $RETVAL
@nicolasgarnil
Copy link

nicolasgarnil commented Apr 30, 2013

Why don't you call the pid file sidekiq.pid instead of the application name?

@leonardteo
Copy link

leonardteo commented Feb 8, 2014

@dyerc Thanks for providing this! There is an issue on line 69. It only greps single digit version numbers and sidekiq currently has a double digit version number, so getting the status always returns that sidekiq is not running when it is.

The fix is to change it to this:
ps -ef | grep 'sidekiq [0-9]*.[0-9]*.[0-9]*' | grep -v grep

Otherwise, this is a great script! Thanks!

Leo

@john-999
Copy link

john-999 commented Apr 12, 2014

@leonardteo - your correction doesn't make it work correctly with versions such as 2.17.7. But if you upgrade to 3.0.0, it works again.

@jamesdbowman
Copy link

jamesdbowman commented Jan 17, 2015

Thank you so much for this!

@StefSchenkelaars
Copy link

StefSchenkelaars commented Mar 31, 2015

An update for the stopping action. It returns a nice message when it is stopped already.

stop() {

  status
  if [ $? -eq 0 ]; then
    echo "Stopping sidekiq message processor .."
    SIG="INT"
    kill -$SIG `cat $PID_FILE`
    RETVAL=$?
    [ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
    return $RETVAL
  else
    echo "Sidekiq message processor is stopped already .."
  fi

}

Also verry helpfull when using capistrano, it will not stop further execution of the script when the worker is not running but will just continue.

@dimidd
Copy link

dimidd commented Feb 25, 2016

Where should APP_CONFIG be used?

@mangust404
Copy link

mangust404 commented Mar 18, 2016

Thank you. I've spent several hours before I've realized that upstart daemon have a error with the launching of sidekiq forks.

@elfassy
Copy link

elfassy commented May 28, 2016

how do you deal with restart?

@ikrauchanka
Copy link

ikrauchanka commented Aug 28, 2016

@elfassy restart = stop + start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment