Instantly share code, notes, and snippets.

Embed
What would you like to do?
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, Redis was not started by default. So I've hacked the following systemd config file. It…
[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

This comment has been minimized.

daurnimator commented Jun 28, 2015

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

@augustf

This comment has been minimized.

augustf commented Oct 5, 2015

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

@akbarahmed

This comment has been minimized.

akbarahmed commented Jan 21, 2016

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

This comment has been minimized.

gomayonqui commented Feb 2, 2016

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

This comment has been minimized.

tholu commented Apr 13, 2016

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

@macdabby

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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
@deniseirvin

This comment has been minimized.

deniseirvin commented Jun 6, 2016

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

This comment has been minimized.

AlexBuitrago commented Sep 21, 2017

  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

This comment has been minimized.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment