96

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?

Vladislav Povorozniuc
  • 2,149
  • 25
  • 26
Yalamber
  • 7,360
  • 15
  • 63
  • 89

4 Answers4

229

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

Evgenia Karunus
  • 10,715
  • 5
  • 56
  • 70
  • 4
    may I ask what's the difference between this and `redis-server &`? – Uduse Mar 22 '18 at 13:00
  • 5
    @Uduse If you ran it with `&` at the end, then when you exited the terminal the server process would be killed. https://stackoverflow.com/a/15595391/6263317 – Jon Deaton Apr 01 '18 at 18:08
  • 1
    @JonDeaton thanks for the explanation, but then what about using `nohup redis-server &` vs `redis-server --daemonize`? I mean, it seems that there's some way to achieve the same functionality without using `--daemonize` and that option is not really needed. – Uduse Apr 03 '18 at 10:47
75

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.

Didier Spezia
  • 70,911
  • 12
  • 189
  • 154
  • I saw this is how you run it as a daemon, but then what command do you use to run it as a daemon? – Michael Fender Feb 19 '14 at 17:49
  • It depends on the platform and OS distribution. – Didier Spezia Feb 19 '14 at 18:52
  • next search result down https://www.digitalocean.com/community/tutorials/how-to-install-and-use-redis – Nick Jul 09 '14 at 11:39
  • 16
    There are a few problems with this answer: 1) it doesn't tell the user where the configuration file is stored. 2) It doesn't say which platform this answer applies to (if there are different locations for the configuration file; at least provide which platform you installed it under to get this result) 3) it doesn't even say what the name of the configuration file is. – George Stocker May 03 '16 at 13:31
  • Your answer gave me an idea, which solved my issue. I had the `daemonize` option set to yes and was trying to keep its Docker container alive with the `-D FOREGROUND` but it was not working and the container was exiting. I got rid of this option and changed the `daemonize` option back to no, and it worked, the container is kept alive. – Stephane Oct 21 '16 at 22:51
  • `daemonize yes` is not available on Windows. See this superuser answer for how to run redis-server on the background without using a service on Windows: https://superuser.com/a/198530/136896. As for Windows versions of redis, there are many flavors, but https://github.com/tporadowski/redis seems the most up-to-date, easily available binary as of Feb 2020 – airstrike Feb 12 '20 at 03:55
  • @GeorgeStocker I know this is old, but to be fair, you can't know which platform the user is running or if he is running a custom setup, so I wouldn't call this a "problem", his answer is correct in *any* platform, that's the point. OP didn't specify OS. – lucaswxp Aug 05 '20 at 19:19
  • sudo nano /etc/redis/redis.conf using this command you can find the config file on ubuntu – Sumesh Es Jul 27 '21 at 10:43
40

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
trex
  • 3,848
  • 4
  • 31
  • 54
7

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)