Skip to content

Instantly share code, notes, and snippets.

@mkocikowski
Last active October 5, 2024 19:50
Show Gist options
  • Save mkocikowski/aeca878d58d313e902bb to your computer and use it in GitHub Desktop.
Save mkocikowski/aeca878d58d313e902bb to your computer and use it in GitHub Desktop.
Setting up Redis to run as a daemon under systemd

This can be used to daemonize anything that would normally run in the foreground; I picked Redis. Put this in /etc/systemd/system/redis.service:

[Unit]
Description=Redis
After=syslog.target

[Service]
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
RestartSec=5s
Restart=on-success

[Install]
WantedBy=multi-user.target

Make sure that redis.conf has demonize no (the default; systemd will take care of 'daemonizing'). The Restart=on-success in the service file means that the daemon will be auto-restarted only when it exited cleanly (so that 'bad' problems are not masked; see doc). Then run:

sudo systemctl enable /etc/systemd/system/redis.service
sudo systemctl start redis.service

Links:

@VirajWadate
Copy link

VirajWadate commented Mar 25, 2019

By default redis server start at boot time you can see by running following command .
● redis.service - Redis In-Memory Data Store

Loaded: loaded (/home/ubuntu/redis/redis.service; enabled; vendor preset: enabled)

Active: active (running) since Mon 2019-03-25 16:40:28 IST; 52min ago
Main PID: 1134 (redis-server)
Tasks: 4
Memory: 7.8M
CPU: 3.022s
CGroup: /system.slice/redis.service
└─1134 /usr/local/bin/redis-server 127.0.0.1:6379

Mar 25 17:27:54 ip-172-31-27-181 redis-server[1134]: 1134:M 25 Mar 17:27:54.478 * 10 changes in 300 seconds. Saving...
Mar 25 17:27:54 ip-172-31-27-181 redis-server[1134]: 1134:M 25 Mar 17:27:54.479 * Background saving started by pid 4319
Mar 25 17:27:54 ip-172-31-27-181 redis-server[1134]: 4319:C 25 Mar 17:27:54.482 * DB saved on disk
Mar 25 17:27:54 ip-172-31-27-181 redis-server[1134]: 4319:C 25 Mar 17:27:54.482 * RDB: 6 MB of memory used by copy-on-write
Mar 25 17:27:54 ip-172-31-27-181 redis-server[1134]: 1134:M 25 Mar 17:27:54.579 * Background saving terminated with success
Mar 25 17:32:55 ip-172-31-27-181 redis-server[1134]: 1134:M 25 Mar 17:32:55.030 * 10 changes in 300 seconds. Saving...
Mar 25 17:32:55 ip-172-31-27-181 redis-server[1134]: 1134:M 25 Mar 17:32:55.030 * Background saving started by pid 4456
Mar 25 17:32:55 ip-172-31-27-181 redis-server[1134]: 4456:C 25 Mar 17:32:55.033 * DB saved on disk
Mar 25 17:32:55 ip-172-31-27-181 redis-server[1134]: 4456:C 25 Mar 17:32:55.034 * RDB: 6 MB of memory used by copy-on-write
Mar 25 17:32:55 ip-172-31-27-181 redis-server[1134]: 1134:M 25 Mar 17:32:55.130 * Background saving terminated with success

Copy link

ghost commented Aug 6, 2020

@mkocikowski,

As @jee1mr said, there is a typo. The setting in redis.conf is:

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize no

and not:

demonize no

@bzkdjc
Copy link

bzkdjc commented Jan 7, 2023

@mkocikowski,

As @jee1mr said, there is a typo. The setting in redis.conf is:

By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. daemonize no

and not:

demonize no

Find here (at Step 3) a somehow detailled way to achieve this task.

@kleajmp
Copy link

kleajmp commented Jun 17, 2024

I was so glad I found this "redis.service" script as the official documentation is still giving instructions to start redis with "init.d"
But after I kept on getting errors when I started the service (status=203/EXEC).
When I started redis-server manually with the command it worked...

Now the problem was the given path in the startup command in the script above:

  • In my case it was NOT: ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
  • But the correct path is: ExecStart=/usr/sbin/redis-server /etc/redis/redis.conf

@richtier
Copy link

richtier commented Oct 5, 2024

note redis version 7 is shipped with redis-server.service, which is enabled during install

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