How to keep redis server running

Redis

Redis Problem Overview


I am using redis for session support in nodejs app. I have installed redis server and it works when I run redis-server, but when I close terminal redis stops and does not work. How do I keep redis server running after closing the terminal?

Redis Solutions


Solution 1 - Redis

And, if you'd like a quick option, run: redis-server --daemonize yes.

Solution 2 - Redis

The easiest way to launch Redis as a daemon is to edit the configuration file and change the following line:

# 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 yes

Be sure to provide the configuration file on the redis-server command line when you launch it.

An example of configuration file is provided in the Redis distribution.

Solution 3 - Redis

As mentioned by @DidierSpezia in his answer,

Set daemonize yes in Redis conf file. Set daemonize yes in Redis conf file at /path/to/redis.conf Generally it should be there at /etc/.

And :

Then trigger redis-server with the conf file as an argument:

./redis-server /etc/redis.conf

UPDATE You may directly run the redis with demonize flag as well

redis-server --daemonize yes

Solution 4 - Redis

The accepted answer is mostly outdated. While the question is old, Google still ranks this highly, so allow me to correct this.

The OP did not provide any detail about his setup, but you can assume it is a linux, and he doesn't mention containers, so you can also assume he is running redis without them.

There is three detail that make the accepted answer a thing to forget

  • Most (popular) distros come with systemd by default
  • Most (popular) distros have redis in their official repos
  • that official redis package installs systemd service for redis

So

  • It will have supervised systemd in its default config
  • To start: the redis daemon with sudo systemctl start redis@instanceName where you substitue "instanceName". Also sudo systemctl enable redis@instanceName for auto-starting on boot. (BTW, forget about service start, and init scripts already! These are less portable nowdays than calling directly systemctl).
  • do NOT set to daemonize: yes, that will interfere with the systemd supervisioning redis!

Systemd will supervise, restart your redis, and you can set service depenedencies and service preconditions to/for it, even for a custom executable it is not that hard, search for systemd unit files (you'll need a ~10 lines config file). Chances are, you'd want it.

If the three detail (making systemd the correct answer) are not met/relevant, you are most likely running redis containerized. For docker/podman/etc., it is another question altogether... (no systemd in the inner linux, but you'd have to (or already do) supervise(d) the container-daemon itself)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYalamberView Question on Stackoverflow
Solution 1 - RedislakesareView Answer on Stackoverflow
Solution 2 - RedisDidier SpeziaView Answer on Stackoverflow
Solution 3 - RedistrexView Answer on Stackoverflow
Solution 4 - RedisKrisztián SzegiView Answer on Stackoverflow