517

I apparently have a redis-server instance running because when I try to start a new server by entering redis-server, I'm greeted with the following:

Opening port: bind: Address already in use

I can't figure out how to stop this server and start a new one.

Is there any command I can append to redis-server when I'm typing in the CLI?

My OS is Ubuntu 10.04.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
Qcom
  • 18,263
  • 29
  • 87
  • 113

30 Answers30

830

Either connect to node instance and use shutdown command or if you are on ubuntu you can try to restart redis server through init.d:

/etc/init.d/redis-server restart

or stop/start it:

/etc/init.d/redis-server stop
/etc/init.d/redis-server start

On Mac

redis-cli shutdown
Shreyas
  • 1,927
  • 17
  • 34
yojimbo87
  • 65,684
  • 25
  • 123
  • 131
  • 8
    You can only do that if you set up redis that way. Kevin McTigue's answer works with minimal setup. – btk Sep 02 '12 at 22:00
  • 1
    On OS X you'll probably have to use `launchctl` to do this, and on other systems, `systemctl` or `service`. – tadman Jul 11 '13 at 15:39
  • 1
    On some machines sudo /etc/init.d/redis stop will work – TroodoN-Mike Apr 09 '14 at 11:35
  • 19
    On modern Ubuntu, I just use `service redis-server stop` and `service redis-server start`, after having installed it with `apt-get install redis-server`. – Cloud Artisans Oct 06 '15 at 02:15
  • 2
    This only works if you have redis-server running as a service. If it is running as a stand alone executable, then you will need to explicitly stop the process. Here are the steps I used to stop the process: pkill redis-server on a linux box –  Sep 08 '17 at 14:27
  • on WSL (Windows subsystem for Linux) ```redis-cli shutdown``` worked for me – Eugenio Miró Apr 02 '19 at 14:43
  • Hi reader, be careful about the `shudown` command flushing your AOF persistence https://redis.io/commands/shutdown – Dimitar Nikovski Jun 12 '19 at 13:43
  • (*) on Mac what worked for me was `brew services stop redis` - the `redis-cli shutdown` was basically a no-op. – jsbueno Jul 21 '22 at 16:58
229

A cleaner, more reliable way is to go into redis-cli and then type shutdown

In redis-cli, type help @server and you will see this near the bottom of the list:

SHUTDOWN - summary: Synchronously save the dataset to disk and then shut down the server since: 0.07

And if you have a redis-server instance running in a terminal, you'll see this:

User requested shutdown...
[6716] 02 Aug 15:48:44 * Saving the final RDB snapshot before exiting.
[6716] 02 Aug 15:48:44 * DB saved on disk
[6716] 02 Aug 15:48:44 # Redis is now ready to exit, bye bye...
Kevin McTigue
  • 3,503
  • 2
  • 18
  • 22
  • 84
    Or don't even start redis-cli. Just send it from the command line via `redis-cli shutdown`. (you can send any command like this, for example `redis-cli set cat dog; redis-cli get cat`) – JesseBuesking Apr 30 '13 at 16:33
  • 7
    The only caveat to @jessebuesking comment is that if you've set up password protection, `redis-cli set cat dog; redis-cli get cat` changes to `redis-cli -a mypassword set cat dog; redis-cli -a mypassword get cat`, which can be annoying after a few commands. – glarrain Jul 17 '13 at 22:20
  • 1
    @glarrain one thing you could do to avoid the redundancy is set an alias for your current bash session, for instance `alias redis-cli="redis-cli -a mypassword"`. This way you can make the calls like in my original comment without having to re-supply your password on each use. – JesseBuesking Nov 27 '13 at 19:48
  • @jessebuesking very creative! Wouldn't have thought about solution. For some reason I'm a little afraid of putting passwords in config files – glarrain Nov 29 '13 at 22:16
  • @glarrain don't take my word for it, but I *think* doing this will only store it in memory for the current session (aka don't add it to your bashrc or bash_profile, just enter it as-is from your prompt). – JesseBuesking Nov 30 '13 at 09:09
  • @JesseBuesking, but the shell may remember a history of all commands that you have entered, just to you can use the "Up" arrow or Ctrl+R. – Sergey Orshanskiy Jun 22 '14 at 06:30
  • @SergeyOrshanskiy this is true. You could go in and delete that entry from your history if you'd like, but your password might still be written to an audit log if it's set up. I'm not a security expert by any means and personally wouldn't do this if you have concerns about security. – JesseBuesking Jun 22 '14 at 21:10
  • +1 to @JesseBuesking: I was getting "(error) ERR operation not permitted" for SHUTDOWN. Passing password using -a made my day. I am a newbie to Redis and was getting frustrated. – Parvez Jul 18 '14 at 08:46
  • Can one similarly restart the redis server via `redis-cli restart` (or something comparable)? – Hassan Baig Mar 05 '17 at 21:57
  • @HassanBaig, no. `redis-cli restart` will report that it can't connect to Redis. – Jochem Schulenklopper Apr 06 '18 at 19:07
  • It is strange though that this option to shutdown Redis is apparently so hard to find (initially, also for me), even though it is rather obvious. That we need to start a `redis-cli` and then type `help @server` and scroll down in a long list... I expected that `redis-cli --help` would have mentioned this as well somewhere. – Jochem Schulenklopper Apr 06 '18 at 19:10
  • As pointed out here: https://stackoverflow.com/questions/50704960/cannot-kill-redis-server-on-linux, this method does not work for everyone; my process kept respawining with a different PID. The accepted answer from @yojimbo87 is the only working method to stop redis service for me. – alelom Dec 04 '18 at 14:49
163

redis-cli shutdown is most effective. The accepted answer does not work for me (OSX Lion). Thanks, @JesseBuesking.

SimplGy
  • 20,079
  • 15
  • 107
  • 144
68

For OSX, I created the following aliases for starting and stopping redis (installed with Homebrew):

alias redstart='redis-server /usr/local/etc/redis/6379.conf'
alias redstop='redis-cli -h 127.0.0.1 -p 6379 shutdown'

This has worked great for local development!

Homebrew now has homebrew-services that can be used to start, stop and restart services. homebrew-services

brew services is automatically installed when run.

brew services start|run redis 
brew services stop redis
brew services restart redis

If you use run, then it will not start it at login (nor boot). start will start the redis service and add it at login and boot.

Ray Hunter
  • 15,137
  • 5
  • 53
  • 51
  • 5
    If you installed redis using homebrew, `brew services stop redis` also works! :) – Edouard Berthe Aug 06 '16 at 06:44
  • I would recommend using *brew services* now for handling the starting and stopping for the defaults. If you need a little more control then there are also other options. – Ray Hunter Aug 06 '16 at 14:52
  • 1
    This is the best answer as it works on all linux flavours and mac, including those installed using apt, yum , brew or downloading. Wether they have upstart or init or system d scripts or not. Thanks – doesnt_matter May 26 '17 at 18:08
  • 1
    I like the name of the aliases :) Cool! – thinklinux Oct 17 '18 at 16:54
  • 1
    what should I do if I want to restart and not shutdown @RayHunter – AATHITH RAJENDRAN May 23 '19 at 10:15
  • what should i do to check the status?? @doesnt_matter – AATHITH RAJENDRAN May 25 '19 at 07:30
  • @AATHITHRAJENDRAN redis-cli does not have a restart command. If you are on mac you can use the homebrew command `brew services restart redis` to do a stop/start which uses `launchctl` under the hood. You can create another alias that uses the commands above to do the stop and start. `alias redrestart='redis-cli -h 127.0.0.1 -p 6379 shutdown && redis-server /usr/local/etc/redis/6379.conf'`. You can also write a function to do it too. – Ray Hunter May 25 '19 at 14:38
  • Sorry for this confusion But `service restart redis_6379` restarts the redis, @ray – AATHITH RAJENDRAN May 26 '19 at 07:32
  • @AATHITHRAJENDRAN that is particular to your system and how it was installed. It will probably be different on different systems. – Ray Hunter May 27 '19 at 03:06
  • works for me. Thanks for answer in here stackoverflow platform – AMIC MING Sep 17 '19 at 19:04
44

stop the redis server type in terminal with root user

sudo service redis-server stop

the message will be display after stop the redis-server

Stopping redis-server: redis-server.

if you want to start the redis-server type

sudo service redis-server start

if you want to restart the server type

sudo service redis-server restart
denny
  • 2,084
  • 2
  • 15
  • 19
  • I wonder if this is still valid for new Redis versions if configured by default. Issuing `service --status-all` won't show redis, hence any `service` command will fail against `redis-server`. But redis does show in `ps -ef`. I just cannot find any related document. – themefield Jun 18 '18 at 23:59
  • Looks on Ubuntu 18.04 `sudo service redis start/stop/status` does the same, both go. – Vladislav Povorozniuc Jan 14 '20 at 21:52
26

Type SHUTDOWN in the CLI

or

if your don't care about your data in memory, you may also type SHUTDOWN NOSAVE to force shutdown the server.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
user1651082
  • 269
  • 3
  • 2
19

Try killall redis-server. You may also use ps aux to find the name and pid of your server, and then kill it with kill -9 here_pid_number.

ciembor
  • 7,189
  • 13
  • 59
  • 100
  • This is great, works on mac / unix. (Mac tested, unix approved) – ThomasReggi Dec 06 '12 at 19:43
  • 31
    Bad idea to hard-kill a DB server. Always prefer a proper shutdown, or at least, kill -15 instead of kill -9. – Zsolt Szilagyi May 29 '13 at 05:58
  • 2
    @ZsoltSzilagy you should provide a link or somewhat of an explanation *why* it's a bad idea. Like how redis does delayed writes, so that there will still be uncommited data in memory. – notbad.jpeg May 30 '14 at 16:55
  • 8
    @notbad.jpeg, of course, you can also drop your computer on the floor. If the table is high enough, this should also stop the server. Do I need a link or somewhat of an explanation as to why this is a bad idea? – Sergey Orshanskiy Jun 22 '14 at 06:34
  • 2
    With this method you're not thinking about the redis database state. You're using an OS functionality to shutdown an application instead of using the application itself or a daemon, which is probably using application functionality too. This solution is like unplugging the cable to shutdown the TV. – Jesus Sep 03 '15 at 13:55
  • Redis has a whole documentation page on what Unix Signals it can handle. "The SIGTERM and SIGINT signals tell Redis to shut down gracefully. When the server receives this signal, it does not immediately exit. Instead, it schedules a shutdown similar to the one performed by the SHUTDOWN command." Signals are given integer values. for most Linux systems, 9 is the SIGKILL signal, which tells the OS to stop the Redis process without allowing Redis to clean up. SIGTERM, or 15, does allow Redis to clean up after itself. https://redis.io/docs/reference/signals/ – uxp Jul 27 '23 at 18:55
18

I would suggest to disable Redis-server, which prevents auto start while computer restarts and very useful to use docker like tools etc.

Step 1: Stop the redis-server

sudo service redis-server stop

Step 2: Disable the redis-server

sudo systemctl disable redis-server

if you need redis, you can start it as:

sudo service redis-server start
Bedram Tamang
  • 3,748
  • 31
  • 27
16

Option 1: go to redis installation directory and navigate to src , in my case :

/opt/redis3/src/redis-cli -p 6379 shutdown

where 6379 is the default port.

Option 2: find redis process and kill

ps aux | grep redis-server

t6b3fg   22292  0.0  0.0 106360  1588 pts/0    S+   01:19   0:00 /bin/sh /sbin/service redis start
t6b3fg   22299  0.0  0.0  11340  1200 pts/0    S+   01:19   0:00 /bin/sh /etc/init.d/redis start

And Then initiate kill:

kill -9 22292

kill -9 22299

I'm using Centos 6.7 , x86_64

hope it helps

App Work
  • 21,899
  • 5
  • 25
  • 38
12

Another way could be:

ps -ef | grep -i 'redis-server'
kill -9 PID owned by redis

Works on *NIX & OSX

smace
  • 1,088
  • 2
  • 11
  • 16
  • 1
    @JoenasE, no, this should not be the accepted answer IMO. Having the OS kill a running process neglects the case that the application might have some data in memory not yet saved to a more persistent store. Shutting down the application _via a method provided by the application_ is preferably a better option. – Jochem Schulenklopper Apr 06 '18 at 19:18
11

if you did make install (e.g ubuntu) while installing redis then you can do:

redis-cli shutdown

as pointed by @yojimbo87 :)

gia huy
  • 329
  • 1
  • 6
  • 12
10

MacOSX - It Worked :)

Step 1 : Find the previously Running Redis Server

ps auxx | grep redis-server

Step 2 : Kill the specific process by finding PID (Process ID) - Redis Sever

kill -9 PID
6

systemd, ubuntu 16.04:

$ sudo systemctl is-active redis-server
active

$ sudo systemctl is-enabled redis-server
enabled

$ sudo systemctl disable redis-server
Synchronizing state of redis-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable redis-server
Removed /etc/systemd/system/redis.service.

$ sudo systemctl stop redis-server
Adobe
  • 12,967
  • 10
  • 85
  • 126
6

Usually this problem arises after I shut down my computer ( or leaving running ) an irregular way.. I believe the port gets stuck open, while the process stops but continues to be bound to the previous port.

9/10 times the fix can be:

$ ps aux | grep redis

-> MyUser 2976  0.0  0.0  2459704    320   ??  S    Wed01PM   0:29.94 redis-server *:6379

$ kill 2976

$ redis-server

Good to go.

SerKnight
  • 2,502
  • 1
  • 16
  • 18
6

Another way could be :

brew services stop redis
Ihor Pavlyk
  • 1,111
  • 13
  • 10
  • +10 for this, it works without apology on mac! this also works ™brew services start redis™ ¢brew services restart redis¢ – Victor.Uduak Jan 25 '19 at 15:12
5

If you know on what port it would be running(by default it would be 6379), you can use below command to get the pid of the process using that port and then can execute kill command for the same pid.

sudo lsof -i : <port> | awk '{print $2}'

the above command will give you pid.

kill <pid>;

This would shutdown your server.

cptjack
  • 379
  • 4
  • 4
5

If you are running redis in a docker container, none of the present answers will help. You have to stop redis container. Otherwise, redis process will keep respawning.

$ docker ps
CONTAINER ID        IMAGE                    PORTS                             
e1c008ab04a2        bitnami/redis:4.0.8-r0   0.0.0.0:6379->6379/tcp

$ docker stop e1c008ab04a2
e1c008ab04a2
Wladek Surala
  • 2,590
  • 1
  • 26
  • 31
5

If you know on which port(default:6379) your redis server is running you can go with option 1 or you can check your redis process and you can kill with option 2

option 1:
Kill process on port:

check     : sudo lsof -t -i:6379
kill      : sudo kill `sudo lsof -t -i:6379`

option 2:
Find the previously Running Redis Server:

 ps auxx | grep redis-server

Kill the specific process by finding PID (Process ID) - Redis Sever

kill -9 PID

Now start your redis server with

redis-server /path/to/redis.conf 
AATHITH RAJENDRAN
  • 4,689
  • 8
  • 34
  • 58
5

If Redis is installed via snap:

sudo snap stop redis.server
Ákos Kovács
  • 502
  • 1
  • 10
  • 23
4

Following worked for me on MAC

 ps aux | grep 'redis-server' | awk '{print $2}' | xargs sudo kill -9
Mahesh Hegde
  • 1,131
  • 10
  • 12
3

In my case it was:

/etc/init.d/redismaster stop
/etc/init.d/redismaster start

To find out what is your service name, you can run:

sudo updatedb
locate redis

And it will show you every Redis files in your system.

joan16v
  • 5,055
  • 4
  • 49
  • 49
3

To stop redis server

sudo service redis-server stop

and check the status of it using

sudo service redis-server status
Madhusudan chowdary
  • 543
  • 1
  • 12
  • 20
Surya Bista
  • 524
  • 6
  • 11
1

The commands below works for me on Ubuntu Server

$ service /etc/init.d/redis_6379 stop
$ service /etc/init.d/redis_6379 start
$ service /etc/init.d/redis_6379 restart
efkan
  • 12,991
  • 6
  • 73
  • 106
1

Redis has configuration parameter pidfile (e.g. /etc/redis.conf - check redis source code), for example:

# If a pid file is specified, Redis writes it where specified at startup
# and removes it at exit.
#
# When the server runs non daemonized, no pid file is created if none is
# specified in the configuration. When the server is daemonized, the pid file
# is used even if not specified, defaulting to "/var/run/redis.pid".
#
pidfile /var/run/redis.pid

If it is set or could be set, instead of searching for process id (pid) by using ps + grep something like this could be used:

kill $(cat /var/run/redis.pid)

If required one can make redis stop script like this (adapted default redis 5.0 init.d script in redis source code):

PIDFILE=/var/run/redis.pid
if [ ! -f $PIDFILE ]
then
    echo "$PIDFILE does not exist, process is not running"
else
    PID=$(cat $PIDFILE)
    echo "Stopping ..."
    kill $PID
    while [ -x /proc/${PID} ]
    do
        echo "Waiting for Redis to shutdown ..."
        sleep 1
    done
    echo "Redis stopped"
fi
Robert Lujo
  • 15,383
  • 5
  • 56
  • 73
1

I don't know specifically for redis, but for servers in general:

What OS or distribution? Often there will be a stop or /etc/init.d/... command that will be able to look up the existing pid in a pid file.

You can look up what process is already bound to the port with sudo netstat -nlpt (linux options; other netstat flavors will vary) and signal it to stop. I would not use kill -9 on a running server unless there really is no other signal or method to shut it down.

ysth
  • 96,171
  • 6
  • 121
  • 214
0

On MacOSX,

This is what worked for me

/etc/init.d/redis restart

/etc/init.d/redis stop

/etc/init.d/redis start
Taku
  • 5,639
  • 2
  • 42
  • 31
0

One thing to check if the redis commands are not working for you is if your redis-server.pid is actually being created. You specify the location of where this file is in

/etc/systemd/system/redis.service 

and it should have a section that looks something like this:

[Service]
Type=forking
User=redis
Group=redis
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
PIDFile=/run/redis/redis-server.pid
TimeoutStopSec=0
Restart=always

Check the location and permissions of the PIDFile directory (in my case, '/run/redis'). I was trying to restart the service logged in as deploy but the directory permissions were listed as

drwxrwsr-x  2 redis    redis      40 Jul 20 17:37 redis

If you need a refresher on linux permissions, check this out. But the problem was I was executing the restart as my deploy user which the permissions above are r-x, not allowing my user to write to the PIDFile directory.

Once I realized that, I logged in using root, reran the restart command on the redis (service redis restart) and everything worked. That was a headache but hopefully this saves someone a little time.

0

To gracefully shutdown specific instances with passwords and not resorting to brute-force kill commands, use:

redis-cli -p <port> -a <pass> shutdown

root@machine:~# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       105  0.1  0.0  60552 10772 ?        Ssl  23:27   0:02 redis-server 127.0.0.1:10002
root       111  0.1  0.0  60552 10900 ?        Ssl  23:28   0:02 redis-server 127.0.0.1:10003
root       117  0.1  0.0  60552 10872 ?        Ssl  23:28   0:02 redis-server 127.0.0.1:10004

root@machine:~# redis-cli -p 10002 -a mypassword shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

root@machine:~# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       111  0.1  0.0  60552 10900 ?        Ssl  23:28   0:02 redis-server 127.0.0.1:10003
root       117  0.1  0.0  60552 10872 ?        Ssl  23:28   0:02 redis-server 127.0.0.1:10004

root@machine:~#

Having a ton of instances warrants writing a batch script to loop through them all for a master shutdown.

OXiGEN
  • 2,041
  • 25
  • 19
0

The service name of redis is redis-server, so you can disable and stop redis with this command:

sudo systemctl disable redis-server 
sudo systemctl stop redis-server 
rahnama7m
  • 865
  • 10
  • 38
-2

You can try this code:

sudo kill -9 $(ps aux | grep 'redis' | awk '{print $2}')
iminiki
  • 2,549
  • 12
  • 35
  • 45