Redis系列---【Redis设置开机自启动】

1.第一种方式

  建议安装的时候用redis/utils/install_service.sh安装,一路下一步默认就行,也可以自定义安装位置。

2.第二种方式

  是在redis安装好之后,不再安装redis,只是添加一个开机自启动。

2.1.修改redis配置文件,redis/etc/redis.conf,把daemonize no改成daemonize yes。

2.2.创建启动脚本

 

vim /etc/init.d/redis

 

复制代码
#!/bin/sh
#Configurations injected by install_server below....

EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid 
CONF="/etc/redis/6379.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
复制代码

这段代码就是redis根目录 /utils/redis_init_script 启动脚本的代码

我这边是用了install_server.sh自动生成的,所以不用改那么多东西,直接用就行

2.3.启动

chmod 775 /etc/init.d/redis #授权
systemctl daemon-reload     #刷新配置
systemctl enable redis      #设置开机自启动
systemctl disable redis     #关闭开机自启动
systemctl start redis       #启动

 

service扩展:

复制代码
格式:chkconfig <service> on
设置指定服务<service>开机时自动启动。"/sbin/chkconfig redis on"

格式:chkconfig <service> off
设置指定服务<service>开机时不自动启动。"/sbin/chkconfig redis on off"

格式:ntsysv
以全屏幕文本界面设置服务开机时是否自动启动。按Table键可以切换到OK和CANCEL
复制代码
 
posted @ 2023-02-27 11:21  雨中上人  阅读(1703)  评论(0编辑  收藏  举报