Run Sidekiq as a service on AWS EC2 server for a Ruby on Rails application
Create a Systemd Service File
Create a systemd service file for Sidekiq. You can create a service file, for example, named sidekiq.service
, in the /etc/systemd/system/
directory, as follows:
sudo nano /etc/systemd/system/sidekiq.service
Add the following content to the service file:
[Unit]
Description=sidekiq
After=syslog.target network.target
[Service]
Type=simple
User=<YOUR_USERNAME>
Group=<GROUP_NAME>
UMask=0002
WorkingDirectory=/path/to/your/rails/app
ExecStart=/bin/bash -lc 'bundle exec sidekiq -C /path/to/your/rails/app/config/sidekiq.yml'
Environment=MALLOC_ARENA_MAX=2
# if we crash, restart
RestartSec=1
Restart=on-failure
StandardOutput=append:/path/to/your/rails/app/log/sidekiq.log
StandardError=append:/path/to/your/rails/app/log/sidekiq.log
# This will default to "bundler" if we don't specify it
SyslogIdentifier=sidekiq
[Install]
WantedBy=multi-user.target
- Replace
YOUR_USERNAME
andGROUP_NAME
with your actual Linux username and group. - Modify the
WorkingDirectory
to point to your Rails application directory. - Adjust the paths for the
ExecStart
,StandardOutput
andStandardError
commands to match your Rails application.
Enable and start the sidekiq service
sudo systemctl enable sidekiq.service
sudo systemctl start sidekiq.service
Check the status
sudo systemctl status sidekiq.service
Check the logs
sudo nano /path/to/your/rails/app/log/sidekiq.log
Whenever you make any changes to the systemd service file, you need to reload the systemd configuration and restart the service for the changes to take effect:
sudo systemctl daemon-reload
sudo systemctl restart sidekiq.service
Now, Sidekiq should be running as a background service managed by systemd on your AWS EC2 instance. It will start on boot, and you can use systemd commands to control and monitor the service.