[database] How can I stop redis-server?

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.

This question is related to database ubuntu redis

The answer is


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


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.


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

To stop redis server

sudo service redis-server stop

and check the status of it using

sudo service redis-server status

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.


You can try this code:

sudo kill -9 $(ps aux | grep 'redis' | awk '{print $2}')

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.


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

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

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.


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.


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

Another way could be :

brew services stop redis

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 

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

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.


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...

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

Another way could be:

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

Works on *NIX & OSX


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

Following worked for me on MAC

 ps aux | grep 'redis-server' | awk '{print $2}' | xargs sudo kill -9

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.


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

redis-cli shutdown

as pointed by @yojimbo87 :)


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


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.


On MacOSX,

This is what worked for me

/etc/init.d/redis restart

/etc/init.d/redis stop

/etc/init.d/redis start

Examples related to database

Implement specialization in ER diagram phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' cannot be loaded Room - Schema export directory is not provided to the annotation processor so we cannot export the schema SQL Query Where Date = Today Minus 7 Days MySQL Error: : 'Access denied for user 'root'@'localhost' SQL Server date format yyyymmdd How to create a foreign key in phpmyadmin WooCommerce: Finding the products in database TypeError: tuple indices must be integers, not str

Examples related to ubuntu

grep's at sign caught as whitespace "E: Unable to locate package python-pip" on Ubuntu 18.04 How to Install pip for python 3.7 on Ubuntu 18? "Repository does not have a release file" error ping: google.com: Temporary failure in name resolution How to install JDK 11 under Ubuntu? How to upgrade Python version to 3.7? Issue in installing php7.2-mcrypt Install Qt on Ubuntu Failed to start mongod.service: Unit mongod.service not found

Examples related to redis

System has not been booted with systemd as init system (PID 1). Can't operate Could not connect to Redis at 127.0.0.1:6379: Connection refused with homebrew How to connect to remote Redis server? WRONGTYPE Operation against a key holding the wrong kind of value php How to store and retrieve a dictionary with redis Start redis-server with config file Linux - Install redis-cli only how to check redis instance version? MISCONF Redis is configured to save RDB snapshots How to get all keys with their values in redis