Run Sidekiq as daemon on Ubuntu

Running as daemon won't restart the sidekiq if it crashes unexpectedly. One alternate way could be to run sidekiq as a service (An upstart job). If the system is rebooted than also the upstart job will run sidekiq. Here is the complete script and method to run sidekiq as a service.

After running sidekiq as a service you can simply start/stop/restart sidekiq by command sudo service sidekiq start/stop/restart.


there's an option to Daemonize sidekiq, just pass -d option

commit


As of sidekiq 6.0 Daemonization wouldnt work and if you pass -d, you'll get a message:

Daemonization mode was removed in Sidekiq 6.0, please use a proper process supervisor to start and manage your services

check the issue here #4045


Daemonization, running a program as Unix, for Sidekiq version 6 or later is not supported. Instead, we have to run the process as a service.

Write a script according to your bundler location, or you can modify the below code snippet and copy the snippet into /usr/lib/systemd/system (CentOS) or /lib/systemd/system (Ubuntu)

[Unit]
Description=sidekiq

After=syslog.target network.target

[Service]

Type=notify
WatchdogSec=10
WorkingDirectory=/home/deploy/apps/project_name

# If you use rbenv:
# ExecStart=/bin/bash -lc 'exec /home/deploy/.rbenv/shims/bundle exec sidekiq -e production'
# If you use the system's ruby:
# ExecStart=/usr/local/bin/bundle exec sidekiq -e production
# If you use rvm in production, don't.

#ExecStart=/home/deploy/.rvm/wrappers/ruby-2.6.5/bundle exec sidekiq -e production

# Use `systemctl kill -s TSTP sidekiq` to quiet the Sidekiq process
# !!! Change this to your deploy user account !!!

User=deploy

Environment=MALLOC_ARENA_MAX=2

# if the script crash, restart
RestartSec=1
Restart=on-failure

# output goes to /var/log/syslog (Ubuntu) or /var/log/messages (CentOS)
StandardOutput=syslog
StandardError=syslog

# This will default to "bundler" if we don't specify it
SyslogIdentifier=sidekiq
[Install]
WantedBy=multi-user.target

Make sure you have given the correct path of your bundler for ExecStart in order to start the process. Save it as sidekiq.service and run systemctl enable sidekiq. Then we can manage the process using the commands systemctl start sidekiq, systemctl stop sidekiq, and systemctl restart sidekiq.

We can see the last 100 lines of log by using journalctl -u sidekiq -rn 100.