0
点赞
收藏
分享

微信扫一扫

Linux Redis 做成系统服务

沪钢木子 2023-05-22 阅读 84

概述

系统:CentOS 7.6

Redis安装后启动方式:redis-server /xxx/xxx/redis.conf。若不指定配置文件,则redis-server不会读取任何配置文件,而是使用自身携带固定配置信息启动。

若想将redis做成系统服务,则需在 /etc/init.d 目录下添加一个配置文件,文件中指定要启动的程序,如何可以使用系统服务的启停方式启动redis。

方法

在/etc/init.d目录下新建/打开文件 redis。

cd /etc/init.d
vim redis

填写如下内容:

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6380
EXEC=/opt/redis/redis-6.2.7/src/redis-server
CLIEXEC=/opt/redis/redis-6.2.7/src/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/opt/redis/redis-6.2.7/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 -a "frwind" -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

打开redis命令: service redis start

关闭redis命令: service redis stop

设为开机启动: chkconfig redis on

设为开机关闭: chkconfig redis off

备注:

可使用 chkconfig --list 查看当前有哪些chkconfig管理的服务。

举报

相关推荐

0 条评论