Redis instance running as background daemon (service)

Petr Kostelanský | 30 October 2017
In this article we are going to look at how create background process for Redis instance and make it automatic runnable after system start.

We are going to create daemon to run Redis instance as background proccess. Daemon is something like service in Windows wolrd.

Redis as daemon under Ubuntu Linux

Let's look at what we need to configure.

Edit /etc/redis/redis-6379.conf file.

The name part ..6379.conf related with port numer under Redis instance running because we are going probably run more Redis instances and each instance need own config file. So we need distinguish those files.

Open config file in editor (open file by Nano editor)

sudo nano /etc/redis/redis-6379.conf

Changes in redis-6379.conf file

Check and set correct port

port 6379

Comment out binding IP address (to comment out use # hash)

# bind

Set protected-mode to no to allow connect to Redis instance remotely

protected-mode no

Find supervised directive and set it to systemd (systemd is linux daemon that start other daemons after system start and exit those daemons when system shutting down)

#   If you run Redis from upstart or systemd, Redis can interact with your
#   supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
#   Note: these supervision methods only signal "process is ready."
#   They do not enable continuous liveness pings back to your supervisor.
supervised systemd

Rename pidfile

pidfile /var/run/

Next find dir and set /var/lib/redis/redis1

# The working directory.
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
# The Append Only File will also be created inside this directory.
# Note that you must specify a directory here, not a file name.
dir /var/lib/redis/redis1

Create systemd file for Redis

Create systemd file in Nano editor

sudo nano /etc/systemd/system/redis1.service

Add text (service is going to be running under redis user)

Description=Redis Cache 1

ExecStart=/usr/local/bin/redis-server /etc/redis/redis-6379.conf
ExecStop=/usr/local/bin/redis-cli -p 6379 shutdown


Create redis user, groups and directories

sudo adduser --system --group --no-create-home redis   
sudo mkdir /var/lib/redis/redis1   # create directory
sudo chown redis:redis /var/lib/redis/redis1   # make redis own directory
sudo chmod 770 /var/lib/redis/redis1  # permission

Start Redis service

sudo systemctl start redis1

Check status

systemctl status redis

--- test redis instance---

Open Redis client and connect to running redis server

redis-cli -p 6379

Test connectivity from Redis client to Redis server>ping

Exit Redis client>exit


Restart Redis service

sudo systemctl restart redis1

Enable Redis to start after boot computer

sudo systemctl enable redis1


Loading ads...