ubuntu系统设置redis为服务

1、编辑服务脚本

进入redis安装目录

cat ./utils/redis_init_script
或
cat ./redis-stable/utils/redis_init_script

 可以看到配置文件

将其拷贝到 /etc/init.d 目录并重命名为 redis(文件路径可能会不同)

cp ./utils/redis_init_script /etc/init.d/redis

修改该文件

 vi  /etc/init.d/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=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
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

修改文件:

#!/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=6379
EXEC=/usr/local/src/redis-6.2.7/src/redis-server
CLIEXEC=/usr/local/src/redis-6.2.7/src/redis-cli

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

主要修改了

  • $EXEC $CONF & ,后边的 &,表示将服务转到后台运行;
  • EXEC、CLIEXEC、CONF等三处路径都要改成实际的安装路径

将安装目录下提供的redis.conf默认配置文件拷贝并重命名为 6379.conf

3、环境变量设置

vim /etc/profile

添加一行,有的版本路径是在bin下,仅供参考

export PATH=/usr/local/redis/src:$PAT
source /etc/profile

验证:

echo $PATH

 可以看到redis的变量配置

4.服务注册

Ubuntu 在 10.04 之前的版本在配置开机启动服务时都是使用 chkconfig,而在之后的版本就没有 chkconfig 命令了:

sudo chkconfig --add redis
 
sudo: chkconfig: command not found

chkconfig 的替换方案为 update-rc.d :

sudo update-rc.d redis defaults

服务启动及验证

sudo service redis stop

sudo service redis start

sudo service redis status

可以看到,redis服务已成功启动

redis-cli ping    

netstat -an|grep 6379

可以看到本地6379端口号已被redis监听

持久化文件和日志

Redis默认的持久化方式是快照方式,默认会将快照 dump.rdb 文件放在启动目录下(服务方式启动是在根目录下),我们可以通过更改 6379.conf 文件里 SNAPSHOTTING 大项下的 dir 配置快照文件放在指定目录下,以防被误删

dir ./
改为自定的路径
dir /xx/xx/

 日志:

Redis 默认将日志输出到 /dev/null(即舍弃),我们可以通过更改 6379.conf 文件里 GENERAL 大项下的 logfile 配置将日志保留到指定文件:

logfile ""
改为自定的路径
logfile /xx/xx/xx/redis.log

重启服务使配置生效

sudo service redis restart

 

 

 
posted @ 2022-07-15 10:46  卷心菜的奇妙历险  阅读(564)  评论(0编辑  收藏  举报