Reputation: 7580
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?
Upvotes: 103
Views: 110155
Reputation: 435
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
So
supervised systemd
in its default configsudo 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
).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)
Upvotes: 10
Reputation: 4057
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
Upvotes: 45
Reputation: 11202
And, if you'd like a quick option, run: redis-server --daemonize yes
.
Upvotes: 239
Reputation: 73286
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.
Upvotes: 76