Redis设置开机启动

1、修改配置文件

[root@localhost ~]# cd Redis/
[root@localhost Redis]# ls
redis-6.2.1  redis-6.2.1.tar.gz
[root@localhost Redis]# cd redis-6.2.1
[root@localhost redis-6.2.1]# ls
00-RELEASENOTES  CONTRIBUTING  INSTALL    README.md       runtest            runtest-sentinel  tests
BUGS             COPYING       Makefile   redis.conf      runtest-cluster    sentinel.conf     TLS.md
CONDUCT          deps          MANIFESTO  redis.conf.bak  runtest-moduleapi  src               utils

修改redis.conf

daemonize yes

2、创建启动脚本,一定按照自己安装的目录修改参数

vi /etc/init.d/redis

#!/bin/sh
#Configurations injected by install_server below....

EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid 
CONF="/root/Redis/redis-6.2.1/redis.conf"
REDISPORT="6379"
###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_6379 is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_6379
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop redis_6379
# Description: Redis daemon
### END INIT INFO


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
        ;;
    status)
        PID=$(cat $PIDFILE)
        if [ ! -x /proc/${PID} ]
        then
            echo 'Redis is not running'
        else
            echo "Redis is running ($PID)"
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Please use start, stop, restart or status as first argument"
        ;;
esac

3、给文件授权

[root@localhost init.d]# chmod 755 redis

4、测试命令

 测试前先重新加载下

[root@localhost init.d]# systemctl daemon-reload

启动

[root@localhost init.d]# systemctl start redis

停止

[root@localhost init.d]# systemctl stop redis

查看状态

[root@localhost init.d]# systemctl status redis
● redis.service - LSB: start and stop redis_6379
   Loaded: loaded (/etc/rc.d/init.d/redis; bad; vendor preset: disabled)
   Active: active (running) since Tue 2023-11-14 21:55:28 EST; 4s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1955 ExecStart=/etc/rc.d/init.d/redis start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/redis.service
           └─1957 /usr/local/bin/redis-server 127.0.0.1:6379

Nov 14 21:55:28 localhost.localdomain systemd[1]: Starting LSB: start and stop redis_6379...
Nov 14 21:55:28 localhost.localdomain redis[1955]: Starting Redis server...
Nov 14 21:55:28 localhost.localdomain systemd[1]: Started LSB: start and stop redis_6379.

设置开机启动

[root@localhost init.d]# chkconfig redis on

查看

[root@localhost init.d]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
redis              0:off    1:off    2:on    3:on    4:on    5:on    6:off

取消开机启动

[root@localhost init.d]# chkconfig redis off

 

 

参考博客:https://www.cnblogs.com/qize/p/17159045.html

 

posted @ 2023-11-15 11:01  中仕  阅读(465)  评论(0编辑  收藏  举报