Running Sidekiq as a Linux Service
I created a service to manage a single Sidekiq process. Below is my configuration that worked on RedHat Linux 7 using systemd. It is based on this example, and should work on CentOS as well.
My Rails application is run under a different user, and so we need lines 29-31. UMask=0002
means that the directory permission is 775 and file permission is 664. See this man page for more configuration options.
I also needed to set the environment variables (lines 27-28) because even though I execute the command on line 42 as the rails_user, the service does not load environment variables specified on the user’s .bash_profile.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# /usr/lib/systemd/system/sidekiq.service
# Customize this file based on your bundler location, app directory, etc.
# Put this in /usr/lib/systemd/system (CentOS) or /lib/systemd/system (Ubuntu).
# Run:
# - systemctl enable sidekiq
# - systemctl {start,stop,restart} sidekiq
#
# This file corresponds to a single Sidekiq process. Add multiple copies
# to run multiple processes (sidekiq-1, sidekiq-2, etc).
#
# See Inspeqtor's Systemd wiki page for more detail about Systemd:
# https://github.com/mperham/inspeqtor/wiki/Systemd
#
[Unit]
Description=sidekiq
# start us only once the network and logging subsystems are available,
# consider adding redis-server.service if Redis is local and systemd-managed.
After=syslog.target network.target
# See these pages for lots of options:
# http://0pointer.de/public/systemd-man/systemd.service.html
# http://0pointer.de/public/systemd-man/systemd.exec.html
[Service]
Type=simple
WorkingDirectory=/some/path/rails_user
ExecStart=/usr/local/rvm/wrappers/ruby-2.3.0/bundle exec sidekiq -e production -L /some/path/rails_user/log/sidekiq.log
Environment=RAILS_ENV=production
Environment=MY_DATABASE_PASSWORD=Some_Password
User=rails_user
Group=rails_user
UMask=0002
# if we crash, restart
RestartSec=1
Restart=on-failure
# output goes to /var/log/syslog
StandardOutput=syslog
StandardError=syslog
# This will default to "bundler" if we don't specify it
SyslogIdentifier=sidekiq
[Install]
WantedBy=multi-user.target