Skip to content

Instantly share code, notes, and snippets.

@elchingon
Last active January 11, 2017 18:08
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 elchingon/b94aa8ca2b175f9ee10da3bb68ea1be4 to your computer and use it in GitHub Desktop.
Save elchingon/b94aa8ca2b175f9ee10da3bb68ea1be4 to your computer and use it in GitHub Desktop.
Ubuntu Sidekiq Service Init.d script
--- /etc/init.d/sidekiq - SAVE File
#!/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
# constants
USER=ubuntu (or whoever)
APP="myapp"
APP_ROOT="/var/www/${APP}"
RAILS_ENV=production
PID_FILE=$APP_ROOT/pids/sidekiq.pid
LOG_FILE="$APP_ROOT/log/sidekiq.log"
LOCK_FILE="$APP_ROOT/${APP}-lock"
DESC="Sidekiq app - $RAILS_ENV"
BUNDLE="bundle"
SIDEKIQ="sidekiq"
START_CMD="$BUNDLE exec $SIDEKIQ -d -L $LOG_FILE -P $PID_FILE -e $RAILS_ENV "
CMD="cd ${APP_ROOT}; ${START_CMD} "
TIMEOUT=60
RETVAL=0
start() {
status
if [ $? -eq 1 ]; then
[ `id -u` == '0' ] || (echo "$SIDEKIQ runs as root only .."; exit 5)
[ -d $APP_ROOT ] || (echo "$APP_ROOT not found!.. Exiting"; exit 6)
cd $APP_ROOT
echo "Starting $SIDEKIQ message processor .. "
su -c "$CMD" - $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 -p `cat $PID_FILE` | grep `cat $PID_FILE`
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
--- END SAVE FILE
-- SETUP Permissions
sudo chmod +x /etc/init.d/sidekiq
--- END SETUP Permissions
--- ADD TO startup
sudo update-rc.d sidekiq defaults
--- END ADD to startup
--- now use it
sudo service sidekiq start|stop|status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment