Linux Redis starts, restarts, starts automatically at boot, and shuts down

1. Start directly:

        1.1. Go to the root directory of the redis installation and execute the following command

        Note: The & symbol can cause redis to start in the background.

nohup redis-server &

2. Start through configuration file

        Example: :/usr/redis/×××.conf

        Enter the redis root directory and enter the following command

./redis-server /usr/redis/×××.conf

  If the port is changed, you need to specify the port when connecting using the redis-cli client, for example:

redis-cli -p 6666

3. Use redis script to set up auto-start at boot

        The startup script redis_init_script is located in the /utils/ directory of redis. The redis_init_script script code is as follows:

        

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
 
#redis服务器监听的端口
REDISPORT=6379
 
#服务端所处位置
EXEC=/usr/local/bin/redis-server
 
#客户端位置
CLIEXEC=/usr/local/bin/redis-cli
 
#redis的PID文件位置,需要修改
PIDFILE=/var/run/redis_${REDISPORT}.pid
 
#redis的配置文件位置,需将${REDISPORT}修改为文件名
CONF="/etc/redis/${REDISPORT}.conf"
 
case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

 According to the startup script, copy the modified configuration file to the specified directory and use the root user to operate:

mkdir /etc/redis
cp redis.conf /usr/redis/×××.conf

Copy the startup script to the /etc/init.d directory. In this example, the startup script is named redisd (usually ending with d to indicate a background self-starting service)

cp redis_init_script /usr/init.d/redisd

Set to auto-start at boot, directly configure and enable auto-start chkconfig redisd on error found: service redisd does not support chkconfig

The solution is to add the following comment at the beginning of the startup script to modify the run level:

#!/bin/sh
# chkconfig:   2345 90 10

Just restart it

#设置为开机自启动服务器
chkconfig redisd on
#打开服务
service redisd start
#关闭服务
service redisd stop

Guess you like

Origin blog.csdn.net/m0_73900214/article/details/131689257