Skip to content

Instantly share code, notes, and snippets.

@hendricius
Last active October 23, 2016 12:39
Show Gist options
  • Save hendricius/ee87772f38902f0e2008 to your computer and use it in GitHub Desktop.
Save hendricius/ee87772f38902f0e2008 to your computer and use it in GitHub Desktop.
Starting Sidekiq via upstart on Ubuntu using rbenv

The Sidekiq service config. Goes into /etc/init/sidekiq.conf

# /etc/init/sidekiq.conf - Sidekiq config

# This example config should work with Ubuntu 12.04+.  It
# allows you to manage multiple Sidekiq instances with
# Upstart, Ubuntu's native service management tool.
#
# See workers.conf for how to manage all Sidekiq instances at once.
#
# Save this config as /etc/init/sidekiq.conf then manage sidekiq with:
#   sudo start sidekiq index=0
#   sudo stop sidekiq index=0
#   sudo status sidekiq index=0
#
# Hack Upstart's reload command to 'quiet' Sidekiq:
#
#   sudo reload sidekiq index=0
#
# or use the service command:
#   sudo service sidekiq {start,stop,restart,status}
#

description "Sidekiq Background Worker"

# This script is not meant to start on bootup, workers.conf
# will start all sidekiq instances explicitly when it starts.
# start on runlevel [2345]
# stop on runlevel [06]

# change to match your deployment user
setuid ubuntu
setgid ubuntu
env HOME=/home/ubuntu

respawn
respawn limit 3 30

# TERM is sent by sidekiqctl when stopping sidekiq. Without declaring these as
# normal exit codes, it just respawns.
normal exit 0 TERM

# Older versions of Upstart might not support the reload command and need
# this commented out.
# reload signal USR1

instance $index

script
# this script runs in /bin/sh by default
# respawn as bash so we can source in rbenv
exec /bin/bash <<'EOT'
        # use syslog for logging
        # exec &> /dev/kmsg
        # pull user rbenv. replace with your home directory. Compared to the suggested config on the github repo, this has to be pulled here.
        export HOME=/home/ubuntu
        export PATH="$HOME/.rbenv/bin:$PATH"
        eval "$(rbenv init -)"
        export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
        cd /home/ubuntu/tripl
        exec bundle exec sidekiq -i ${index} -e production
EOT
end script

Now we don't want to start the workers individually. We also want to go ahead and have all of them started on bootup of the system.

Create a new /etc/init/workers.conf


# /etc/init/workers.conf - manage a set of Sidekiqs

# This example config should work with Ubuntu 12.04+.  It
# allows you to manage multiple Sidekiq instances with
# Upstart, Ubuntu's native service management tool.
#
# See sidekiq.conf for how to manage a single Sidekiq instance.
#
# Use "stop workers" to stop all Sidekiq instances.
# Use "start workers" to start all instances.
# Use "restart workers" to restart all instances.
# Crazy, right?
#

description "manages the set of sidekiq processes"

# This starts upon bootup and stops on shutdown
start on runlevel [2345]
stop on runlevel [06]

# Set this to the number of Sidekiq processes you want
# to run on this machine
env NUM_WORKERS=2

pre-start script
  for i in `seq 1 ${NUM_WORKERS}`
  do
    start sidekiq index=$i
  done
end script

post-stop script
  for i in `seq 1 ${NUM_WORKERS}`
  do
    stop sidekiq index=$i
  done
end script

Voila, sudo stop workers and sudo start workers should work.

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