Skip to content

Instantly share code, notes, and snippets.

@rikas
Forked from alain75007/sidekiq.init.sh
Last active December 21, 2015 00:28
Show Gist options
  • Save rikas/6220043 to your computer and use it in GitHub Desktop.
Save rikas/6220043 to your computer and use it in GitHub Desktop.
#!/bin/bash
### BEGIN INIT INFO
# Provides: sidekiq beta
# Required-Start: $syslog $remote_fs
# Required-Stop: $syslog $remote_fs
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: sidekiq
# Description: sidekiq
### END INIT INFO
#Variable Set
NAME=sidekiq
SIDEKIQ="sidekiq"
APP="gleam-backend"
APP_DIR="/home/gleamworld/$APP/current"
APP_CONFIG="$APP_DIR/config/sidekiq.yml"
LOG_FILE="$APP_DIR/log/sidekiq.log"
LOCK_FILE="$APP_DIR/sidekiq-lock"
PIDDIR="$APP_DIR/tmp/pids"
PID_FILE="$PIDDIR/sidekiq.pid"
APP_ENV="production"
RAILS_ENV="production"
#### !IMPORTANT PIDFILE an LOGFILE should be defined in RAISL_ROOT/config/sidekiq.yml
### so not sure -P and -L are necessary here :
START_CMD="/usr/local/bin/bundle exec $SIDEKIQ -d -e $APP_ENV -P $PID_FILE -L $LOG_FILE -C $APP_CONFIG"
RETVAL=0
start() {
status
if [ $? -eq 1 ]; then
[ `id -u` -eq 0 ] || (echo "Sidekiq runs as root only!"; exit 5)
[ -d $APP_DIR ] || (echo "$APP_DIR not found!"; exit 6)
cd $APP_DIR
echo "Starting sidekiq..."
$START_CMD >> $LOG_FILE 2>&1
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 is already running."
fi
}
stop() {
echo "Stopping sidekiq..."
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]* $APP" | grep -v grep
return $?
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
if [ $? -eq 0 ]; then
echo "Sidekiq is running."
RETVAL=0
else
echo "Sidekiq is stopped."
RETVAL=1
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 0
;;
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment