我显然有一个redis-server实例正在运行,因为当我试图通过输入redis-server启动一个新服务器时,我受到以下欢迎:

Opening port: bind: Address already in use

我不知道如何停止这个服务器,并启动一个新的。

当我在CLI中输入时,是否有任何命令可以附加到redis-server ?

我的操作系统是Ubuntu 10.04。


当前回答

Redis有配置参数pidfile(例如/etc/redis.conf -检查Redis源代码),例如:

# 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

如果它已经设置或可以设置,可以使用ps + grep来搜索进程id (pid),而不是像这样使用:

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

如果需要,一个可以使redis停止脚本像这样(改编默认redis 5.0初始化。D脚本在redis源代码):

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

其他回答

我建议禁用Redis-server,它可以防止计算机重启时自动启动,对使用docker之类的工具非常有用。

步骤1:停止redis-server

sudo service redis-server stop

步骤2:关闭redis-server

sudo systemctl disable redis-server

如果你需要redis,你可以这样启动:

sudo service redis-server start

Redis有配置参数pidfile(例如/etc/redis.conf -检查Redis源代码),例如:

# 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

如果它已经设置或可以设置,可以使用ps + grep来搜索进程id (pid),而不是像这样使用:

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

如果需要,一个可以使redis停止脚本像这样(改编默认redis 5.0初始化。D脚本在redis源代码):

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

下面的命令适用于我的Ubuntu服务器

$ service /etc/init.d/redis_6379 stop
$ service /etc/init.d/redis_6379 start
$ service /etc/init.d/redis_6379 restart

如果您知道它将在哪个端口上运行(默认情况下是6379),您可以使用以下命令获取使用该端口的进程的pid,然后可以对相同的pid执行kill命令。

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

上面的命令将为您提供pid。

杀死pid > <;

这将关闭您的服务器。

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