Sidekiq Upstart Job or Sidekiq as a service
Sidekiq Upstart Job
Running Sidekiq as a Service
How to run sidekiq automatically if sidekiq stops running
sudo vim /etc/init/sidekiq.conf
Now copy and paste the following code in the file:
# Just a custom description for our Job
description "Sidekiq Background Worker"
# On which conditions the job should start. In this case it's very simple: On the system startup (this is basically when the system is booted)
#start on startup
start on runlevel [2345]
# On which conditions the job should stop. In this case when the system reboot (http://upstart.ubuntu.com/cookbook/#runlevels)
stop on runlevel [06]
# This are the User and User Group that will be used to run the Job. On our case it should be the user that we have set on our capistrano script for instance.
# You can check at `config/deploy/<environment>.rb` on this line `server <some_ip_addreess>, user: <deploy_user>`
setuid ubuntu
setgid ubuntu
# This indicate that we want to restart the Job if it crashes
respawn
respawn limit 5 10
# TERM is sent by sidekiqctl when stopping sidekiq. Without declaring these as normal exit codes, it just respawns.
normal exit 0 TERM
script
# this script runs in /bin/sh by default
# respawn as bash so we can source in RVM
exec /bin/bash <<EOT
# use syslog for logging
exec &> /dev/kmsg
# Jump into the capistrano deployment directory
cd /home/ubuntu/myproject/
# Start Sidekiq through RVM. Note that I'm using the standard Capistrano paths
exec ~/.rvm/bin/rvm-shell -c 'bundle exec sidekiq -C ./config/sidekiq.yml --environment production --logfile /home/ubuntu/myproject/log/sidekiq.log'
EOT
end script
The script is itself self explanatory. You just need to replace the project directory in place of myproject and the environment in which it runs like here it is the production environment. One more thing to note here is the log file of sidekiq is /home/ubuntu/myproject/log/sidekiq.log and similarly the conf file is /home/ubuntu/myproject/config/sidekiq.yml, so change these as per your project scaffolding.
Now save and exit. We are almost done now we need to start this job by running the following command.
sudo service sidekiq start
This will start the sidekiq service which you can see by running the following command
sudo service sidekiq status
And we can see that sidekiq is also running:
ps aux | grep sidekiq
You can now kill the sidekiq process by the process id shown in the result of above command and wait a bit and keep the status again if sidekiq is running or not. You will notice that the sidekiq has started again after we killed the sidekiq process.
Try this out to get rid of checking again and again whether sidekiq is running or not. If you find any difficulty or have any doubt in the above procedure than your doubts are welcome in comments.
the script is look great, thank Ajeet 🙂
Glad that it helped you.
Failed to start sidekiq.service: Unit sidekiq.service not found
I am getting this error
Please save this upstart as sidekiq.conf, so that it will be available as a service with "sidekiq" name. Try saving it in /etc/init.d file if you are using RHEL based OS.
Thank you for this guide, very helpful! But please, can you add as an alternative a call of sidekiq via rbenv? Here is what to add:
—
# Start Sidekiq through RBENV. Note that I'm using the standard Capistrano paths
/home/ubuntu/.rbenv/shims/bundle exec sidekiq -c 1 -L log/sidekiq.log