Setting up Sidekiq as a Systemd service in Ubuntu 22

Add systemd service

sudo vim /etc/systemd/system/sidekiq.service

WorkingDirectory: the path to your working directory
ExecStart: Using option -e to set ENV. Eg: ExecStart=/bin/bash -lc 'bundle exec sidekiq -e staging'
StandardOutput, StandardError: the path to sidekiq log file

After=syslog.target network.target

[Service]

Type=notify
# If your Sidekiq process locks up, systemd's watchdog will restart it within seconds.
WatchdogSec=10

WorkingDirectory=/var/www/convoy-official-api/current

# 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 without gemset and your ruby version is 2.6.5
# ExecStart=/home/deploy/.rvm/gems/ruby-2.6.5/wrappers/bundle exec sidekiq -e production
# If you use rvm in production with gemset and your ruby version is 2.6.5
# ExecStart=/home/deploy/.rvm/gems/ruby-2.6.5@gemset-name/wrappers/bundle exec sidekiq -e production
# If you use rvm in production with gemset and ruby version/gemset is specified in .ruby-version,
# .ruby-gemsetor or .rvmrc file in the working directory
ExecStart=/bin/bash -lc 'bundle exec sidekiq -e production'

# Use `systemctl kill -s TSTP sidekiq` to quiet the Sidekiq process

# Greatly reduce Ruby memory fragmentation and heap usage
# https://www.mikeperham.com/2018/04/25/taming-rails-memory-bloat/
Environment=MALLOC_ARENA_MAX=2

# if we crash, restart
RestartSec=1
Restart=always

# output goes to /var/log/syslog (Ubuntu) or /var/log/messages (CentOS)
StandardOutput=file:/var/www/convoy-official-api/current/log/sidekiq.log
StandardError=file:/var/www/convoy-official-api/current/log/sidekiq.log

# This will default to "bundler" if we don't specify it
SyslogIdentifier=sidekiq

[Install]
WantedBy=multi-user.target

Load new config
sudo systemctl daemon-reload

Enable Sidekiq service
sudo systemctl enable sidekiq

Start sidekiq
sudo service sidekiq start

Check Sidekiq status
sudo service sidekiq status