Sidekiq process in production with Systemd and Monit

Recently, we have upgraded our Sidekiq version from 5.2 to 6.5. Before Sidekiq 6.0 we were managing the Sidekiq process directly using Monit. With the release of Sidekiq 6, the team has removed the daemonization, logfile, and pidfile command line arguments and sidekiqctl binary. Managing services manually is more error-prone, let our operating system do it for us. We have three options to go with systemd, upstart, and foreman. We decided to go ahead with the systemd.


Systemd is a system and service manager for linux. Systemd tasks are organized as units. Most common units are services(.service), mount points(.mount), devices(.device), sockets(.socket), or timers(.timer)


The systemctl command is a utility which is responsible for examining and controlling the systemd system and service manager.


Simple, efficient background processing for Ruby.

Sidekiq running as Systemd Service

  1. To manage Sidekiq we need to create a service file for Sidekiq which can be used to start, stop or restart the Sidekiq process.
    Sudo nano /lib/systemd/system/sidekiq.service
  2. Content in the Sidekiq.service. Sidekiq has provided us with the template for the service file here Sidekiq.service. We modified it according to our use case
    # If your Sidekiq process locks up, systemd's watchdog will restart it within seconds.
    ExecStart=/usr/local/bin/bundle exec sidekiq -C /opt/myapp/shared/config/sidekiq.yml -e production
    ExecStop=/bin/kill -TSTP $MAINPID
    ExecStartPost=/bin/sh -c '/bin/echo $MAINPID > /opt/myapp/shared/pids/'
    ExecStopPost=/bin/sh -c 'rm /opt/myapp/shared/pids/'
    # Greatly reduce Ruby memory fragmentation and heap usage
    # if we crash, restart
    # output goes to /var/log/syslog (Ubuntu) or /var/log/messages (CentOS)
    # This will default to "bundler" if we don't specify it
  3. Our Modified Configurations:
    • As we were using system ruby and using Sidekiq with some custom configurations. To start Sidekiq we used.
      ExecStart=/usr/local/bin/bundle exec sidekiq -C /opt/myapp/shared/config/sidekiq.yml -e production
    • To stop Sidekiq we need to send a TSTP signal to process all the busy jobs before terminating Sidekiq.
      ExecStop=/bin/kill -TSTP $MAINPID
    • For Managing with Monit we need the process id, After starting or stopping the service we were maintaining the process id file.
      ExecStartPost=/bin/sh -c '/bin/echo $MAINPID > /opt/myapp/shared/pids/'
      ExecStopPost=/bin/sh -c 'rm /opt/myapp/shared/pids/'
    • As we want to use our app user to run this service.
    • And we want to restart only when there is a failure.
      # if we crash, restart
  4. Reload the systemctl daemon for the new created service
    Sudo systemctl daemon-reload
  5. Now we can start the Sidekiq service:
    sudo systemctl start|stop|restart sidekiq

Monitor Sidekiq process with Monit

Now we have systemd to start, stop and restart the Sidekiq process. Now we will look at how to monitor the Sidekiq process with the help of monit.


Monit is a utility for managing and monitoring processes, programs, files, directories and filesystems on a Unix system.

  1. Modified monitrc
    check process sidekiq with pidfile "/opt/myapp/shared/pids/"
      start program = "/bin/bash -l -c  'sudo systemctl start sidekiq' as uid deploy and gid deploy"
        with timeout 20 seconds
      stop program  = "/bin/bash -l -c  'sudo systemctl stop sidekiq' as uid deploy and gid deploy"
        with timeout 20 seconds
      if totalmem is greater than 800 MB for 3 cycles then restart
      if changed pid then exec "/etc/monit/"
      if cpu is greater than 65% for 2 cycles then exec "/etc/monit/" else if succeeded then exec "/etc/monit/"
  2. We can check if sidekiq is up and running:
    sudo monit summary sidekiq

Monit will check the Sidekiq process and it will automatically start in case of the unexpected kill of the Sidekiq process.

We have successfully completed the Sidekiq process monitoring with the help of Monit and Systemd.

Manish Sharma photo Manish Sharma
Manish Sharma is a member of Technology at eLitmus. He is passionate about building stuffs. Loves to spend time with nature and swimming.