2015-03-03

RVM Rails Sidekiq as Daemon : /etc/init.d/sidekiq

source from : http://cdyer.co.uk/blog/init-script-for-sidekiq-with-rbenv

首先,找到bundle wapper,類似: /home/user/.rvm/wrappers/ruby-2.1.5/bundle
然後Project path : /home/user/project/my_rails_project

再來,sudo vim /etc/init.d/sidekiq,輸入以下內容

!/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="my_rails_project"  #fix this
AS_USER="user"  #fix this
APP_DIR="/home/user/project/my_rails_project"  #fix this

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="/home/user/.rvm/wrappers/ruby-2.1.5/bundle"  #fix this

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

之後就輸入一些指令讓它可以開機執行
sudo update-rc.d sidekiq defaults 99

然後就可以開心的這樣做
service sidekiq start
service sidekiq status
service sidekiq stop

沒有留言:

張貼留言