如何保持 Redis 服务器运行

我在 nodejs 应用程序中使用 Redis 来支持会话。我已经安装了 redis 服务器,它工作时,我运行 redis-server,但当我关闭终端 redis 停止,不工作。关闭终端后如何保持 Redis 服务器运行?

89703 次浏览

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.

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

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

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)