Starting Redis in systemd with Auth

Nathan Thomas asked:

Does anyone know how to pass the auth to redis “when it is started in systemd.” You have to use a custom init script here. Mine is the following:

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

[Service] 
User = redis 
ExecStart = /www/redis/redis-4.0.11/src/redis-server /www/redis/tests/redis.conf 
ExecStop = /www/redis/redis-4.0.11/src/redis-cli shutdown 
RestartSec = 3s 
Restart = on-success

[Install] 
WantedBy = multi-user.target

I know how to do it if I use the redis_init_script found in redis/utils, but it has to be placed in the /etc/init.d. It does not work when placed in the lib/systemd/system.

My answer:


You can create a systemd service override for this. For example:

# systemctl edit redis.service

[Service]
ExecStop=
ExecStop=/www/redis/redis-4.0.11/src/redis-cli -a mypassword shutdown

The first ExecStop= is necessary to discard the list of ExecStop= commands from the original unit.


Note that this is a custom redis systemd unit. The redis systemd unit that ships with Fedora (and the identical package built by remi for RHEL/CentOS) calls a shutdown script that reads the password from redis.conf and automatically passes it in redis-cli, so no customization is necessary. You might wish to use the distribution redis, or at least study its scripts.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.