将Redis设置为开机后台启动

八角塘塘主 2019-12-09

今天周一,一大早来公司,就听到开发的同学说本地项目起不来了, 上周六加班儿还好好的, 应该是周日断电导致的.

redis是关闭的,redis没有设置开机自启动,于是就先把问题先解决吧,然后写个脚本设置redis开机自启动。

1、在写脚本前设置下redis允许在后台启动,修改redis.conf文件,把daemonize no改成daemonize yes

将Redis设置为开机后台启动

将Redis设置为开机后台启动

2、开始编写脚本

#vim /etc/init.d/redis  --创建脚本文件

REDISPORT=6379
EXEC=/usr/local/software/redis-4.0.9/redis-server
CLIEXEC=/usr/local/software/redis-4.0.9/redis-cli

PIDFILE=/data/redis-data/redis-6379/redis.pid
CONF="/data/redis-data/redis-6379/conf/redis.conf"

这里我的redis安装路径是/usr/local/software/redis-4.0.9,端口为6379,然后redis.conf文件在/data/***/conf/redis.conf下面,你们根据自己的实际情况修改就行(这个脚本可以根据redis自带的启动脚本进行修改,它默认的路径是在$redis/utils/redis_init_script,$redis是redis的安装路径)

#!/bin/sh
#chkconfig: 2345 80 05
#description: Start and Stop redis

REDISPORT=6379
EXEC=/usr/local/software/redis-4.0.9/redis-server
CLIEXEC=/usr/local/software/redis-4.0.9/redis-cli

PIDFILE=/data/redis-data/redis-6379/redis.pid
CONF="/data/redis-data/redis-6379/conf/redis.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
        ;;
    restart)
        "$0" stop
        sleep 3
        "$0" start
        ;;
    *)
        echo "Please use start or stop or restart as first argument"
        ;;
esac

3、写完记得修改文件权限

#chmod +x /etc/init.d/redis

4、把脚本添加到系统服务列表

#chkconfig --add redis
#chkconfig redis
#chkconfig --list
#chkconfig --help // 这里如果上传的redis启动文件有问题, 可以通过help命令具体操作;

相关推荐

DiamondTao / 0评论 2020-08-30