Skip to content

Instantly share code, notes, and snippets.

@geschke
Last active January 10, 2021 20:11
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save geschke/ab6afa91b2d9dfcd5c25 to your computer and use it in GitHub Desktop.
Save geschke/ab6afa91b2d9dfcd5c25 to your computer and use it in GitHub Desktop.
WARNING. OUTDATED! The information on this page is severely out of date. Don't use it! Ubuntu systemd service file for Redis. The server was updated to a Ubuntu 15.04 (Vivid Vervet) distribution. Redis was installed from the PPA from Roman Wookey (https://launchpad.net/~rwky/+archive/ubuntu/redis). After the switch to systemd instead of Upstart,…
[Unit]
Description=Redis Datastore Server
After=network.target
[Service]
Type=forking
PIDFile=/var/run/redis/redis.pid
ExecStartPre=/bin/mkdir -p /var/run/redis
ExecStartPre=/bin/chown redis:redis /var/run/redis
ExecStart=/sbin/start-stop-daemon --start --chuid redis:redis --pidfile /var/run/redis/redis.pid --umask 007 --exec /usr/bin/redis-server -- /etc/redis/redis.conf
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
@daurnimator
Copy link

Please don't suggest placing files in /lib. Place in /etc/systemd/systemd instead

@augustf
Copy link

augustf commented Oct 5, 2015

At least on Debian, /lib/systemd/system is the proper location. As always, mileage varies across distributions.

@akbarahmed
Copy link

With systemd, the package (.deb or .rpm) should install the *.service file in /lib/systemd/system/ (Ubuntu) or in the /usr/lib/systemd/system/ (RHEL) directory. So the script above should go into /lib/systemd/system/.

Then to enable the script a symlink is created in /etc/systemd/system/[TARGET].target.wants/, such as /etc/systemd/system/multi-user.target.wants/. The symlink is created with systemctl enable [name].service, such as systemctl enable redis.service.

Finally, if an administrator wants to customize the service file then he/she should create a copy in /etc/systemd/system/[SERVICE-NAME].service. For example (on Ubuntu):

cp /lib/systemd/system/redis.service /etc/systemd/system/redis.service

@gomayonqui
Copy link

I tried exactly as described
Ubuntu 15.04

With the script in /lib/systemd/system/redis-server.service

When I try to restart the start/restart the service it get stucked with this message:
Job for redis-server.service failed. See "systemctl status redis-server.service" and "journalctl -xe" for details.

$ sudo systemctl status redis-server.service

systemctl status redis-server.service
● redis-server.service - Redis Datastore Server
   Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
   Active: failed (Result: timeout) since Tue 2016-02-02 09:58:07 EST; 8min ago
  Process: 8155 ExecStart=/sbin/start-stop-daemon --start --chuid redis:redis --pidfile /var/run/redis/redis.pid --umask 007 --exec /usr/bin/redis-server -- /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
  Process: 8152 ExecStartPre=/bin/chown redis:redis /var/run/redis (code=exited, status=0/SUCCESS)
  Process: 8149 ExecStartPre=/bin/mkdir -p /var/run/redis (code=exited, status=0/SUCCESS)
 Main PID: 7809 (code=exited, status=0/SUCCESS)

Any thoughts?

@tholu
Copy link

tholu commented Apr 13, 2016

@gomayonqui I have a similar problem on Debian. Did you solve it?

@macdabby
Copy link

macdabby commented May 4, 2016

I had several problems with this. My solution is below. I'm really new to systemd, so i'll be a little more verbose for other beginners. My example below is working on debian jessie (8) with redis 3.0.7

Final startup script in /lib/systemd/system/redis.service:

[Unit]
Description=Redis Datastore Server
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis/redis.pid
User=redis
Group=redis

ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

The redis conf file has to be updated as well. Make sure it's in the right location as above. You will need to update the following:

daemonize yes # this has a default to no, and will hand the systemctl process if you try to start it
pidfile /var/run/redis/redis.pid # this defaults to /var/run/redis.pid. but the redis user can't write to that which is why the systemd script has it in a directory. make sure to create /var/run/redis and chown to redis:redis
logfile /var/log/redis.log # i had to touch the file and chown to redis:redis
dir /var/redis # this defaults to "./". i'm not sure which directory this is when started from systemd, but it's not writable. so set the dir explicitly here, and make sure to create /var/redis and chown to redis:redis

@macdabby
Copy link

macdabby commented May 4, 2016

btw, restart seems to work, but reload doesn't. I looked in the redis docs and I don't think it implements any way to reload, although there are commands to modify the config once it's loaded. But that doesn't reload an entire config file.

The log when i reload:

● redis.service - Redis Datastore Server
   Loaded: loaded (/lib/systemd/system/redis.service; disabled)
   Active: active (running) since Wed 2016-05-04 11:49:02 CDT; 4s ago
  Process: 1989 ExecStop=/usr/local/bin/redis-cli shutdown (code=exited, status=1/FAILURE)
  Process: 1986 ExecReload=/bin/kill -USR2 $MAINPID (code=exited, status=0/SUCCESS)
  Process: 1992 ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
 Main PID: 1993 (redis-server)
   CGroup: /system.slice/redis.service
           └─1993 /usr/local/bin/redis-server *:6379

@hos7ein
Copy link

hos7ein commented May 8, 2016

@macdabby
tnx , it's work for me ;)

content of /usr/lib/systemd/system/redis.service file :

[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis/redis.pid
ExecStart=/usr/bin/redis-server /etc/redis.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/usr/bin/redis-shutdown
User=redis
Group=redis

[Install]
WantedBy=multi-user.target

and on /etc/redis.conf file I edit this line :

daemonize yes
pidfile /var/run/redis/redis.pid

@udf2457
Copy link

udf2457 commented May 22, 2016

May I suggest this is cleaner than the original :

[Unit]
Description=Redis Datastore Server
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis/redis_6379
User=redis
Group=redis

Environment=statedir=/var/run/redis
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p ${statedir}
ExecStartPre=/bin/chown -R redis:redis ${statedir}
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

@denise-sanders
Copy link

If you are having trouble getting macdabby's code to run, check the logfile you have specified in /etc/redis/redis.conf. It is helpful if you missed a permission or chown

@AlexBuitrago
Copy link

  1. sudo vim /etc/systemd/system/redis.service

  2. Add this in the file:

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

Type=forking

@raysarebest
Copy link

raysarebest commented Dec 22, 2017

Just to toss my hat in this ring, this is the unit file that ended up working for me. I'm using a self-compiled Redis, v=4.0.6 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=f5447a0fadf7b78a running on Ubuntu 16.04:

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis/redis.pid

User=redis
Group=redis

RuntimeDirectory=redis
RuntimeDirectoryMode=0700

ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

You have to edit /etc/redis/redis.conf to include these lines:

daemonize yes
pidfile /var/run/redis/redis.pid

@sshymko
Copy link

sshymko commented Feb 17, 2019

Just wanted to add that Redis server parameters, such as daemonize no, can be passed as CLI arguments --daemonize no.
Having all the settings in the service declaration file releases from maintaining consistency across multiple config files.

See the example: Gist.

@tharun208
Copy link

What configuration should I add in the [Unit] section to make the service-disabled by default. Or any other options to make this by code will be really helpful

@geschke
Copy link
Author

geschke commented Jan 10, 2021

Just a remark: The original gist is really, really old. Ubuntu has reached the 20.10 version, so today these hints are outdated.
If you want to handle a systemd service, read the instructions for systemctl. To disable a service, a command like
"sudo systemctl disable " will be used.
And/or use Google to search for recent documentation, there are plenty of websites which describe systemd/systemctl, e.g. here: https://linoxide.com/linux-how-to/enable-disable-services-ubuntu-systemd-upstart/

Thanks & Kind regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment