Redis启停脚本

目录

1.概述

解决部分老系统,如Centos6上启停Redis服务的问题。

2.Redis自带脚本

  • Redis安装包下载
bash 复制代码
wget http://download.redis.io/releases/redis-6.2.9.tar.gz
  • 脚本存放在解压后安装包路径下utils目录中,redis_init_script与redis_init_script.tpl两个文件
bash 复制代码
[root@centos utils]# pwd
# 请将/home/redis-6.2.9替换为安装包解压路径
/home/redis-6.2.9/utils
# 查看服务管理脚本
[root@centos utils]# ll redis_init_script*
-rwxrwxr-x 1 root root 1352 5月  19 14:12 redis_init_script
-rwxrwxr-x 1 root root 1047 5月  19 14:12 redis_init_script.tpl

3.Redis安装

参见我另一篇笔记: Redis单例部署

4.修改Redis默认脚本

bash 复制代码
vim /etc/init.d/redis-server
bash 复制代码
#!/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

# 服务主目录
REDIS_HOME=/usr/local/redis
# 服务端口
REDISPORT=6379
# 服务端程序路径
EXEC=${REDIS_HOME}/bin/redis-server
# 客户端程序路径
CLIEXEC=${REDIS_HOME}/bin/redis-cli

# PID文件路径,注意与redis.conf配置文件中保持一致
PIDFILE=/var/run/redis/redis_${REDISPORT}.pid
# 配置文件路径
CONF="${REDIS_HOME}/redis.conf"

# 定义运行服务的普通用户,需要提前创建
USER="redis"
#if [ `whoami` != "${USER}" ]; then
#        exec su - "${USER}"
#fi
# 定义sudo命令
CMD="/usr/bin/sudo -u ${USER}"


case "$1" in
    # 启动服务
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                # 使用普通用户启动服务
                exec $CMD $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
                # 因为Redis设置了密码,所以修改停止服务命令。
                /bin/kill -s TERM ${PID}
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                /bin/rm -rf $PIDFILE
                echo "Redis stopped"
        fi
        ;;
    # 服务状态
    status)
        if [ ! -f $PIDFILE ]
        then
            echo 'Redis is not running'
        else
            PID=$(cat $PIDFILE)
            echo "Redis is running ($PID)"
        fi
        ;;
    # 重启服务
    restart)
        $0 stop
        sleep 1
        $0 start
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

5.注册为Service

检验脚本/etc/init.d/redis-server

  • 确保程序和配置文件路径正确
  • 确保端口是redis服务监听端口(会使用pid文件,pid文件与redis.conf中配置需一致)。

脚本添加执行权限

bash 复制代码
chmod +x /etc/init.d/redis-server

添加到redis-server服务

bash 复制代码
chkconfig --add redis-server

启用redis-server服务

bash 复制代码
# chkconfig redis-server on

6.功能验证

启动服务

bash 复制代码
[root@centos utils]# service redis-server start
Starting Redis server...
bash 复制代码
# 服务使用普通用户(redis)运行
[root@centos init.d]# ps -ef | grep 6379 | grep -v grep
redis    12530     1  0 18:54 ?        00:00:00 /usr/local/redis/bin/redis-server *:6379     

服务状态

bash 复制代码
[root@centos utils]# service redis-server status
Redis is running (11304)
bash 复制代码
[root@centos utils]# netstat -nltp | grep 6379 | grep -v grep
tcp        0      0 0.0.0.0:6379               0.0.0.0:*                   LISTEN      11304/redis-server  
tcp        0      0 :::6379                    :::*                        LISTEN      11304/redis-server  

重启服务

bash 复制代码
[root@centos utils]# service redis-server restart
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...

停止服务

bash 复制代码
[root@centos utils]# service redis-server stop
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
相关推荐
李少兄18 小时前
MySQL 数据库表数量统计
数据库·mysql·oracle
某个默默无闻奋斗的人1 天前
高性能 MySQL 进阶:索引核心原理、失效场景与底层优化全解
数据库·mysql
极限实验室1 天前
INFINI Labs 产品更新 - Easysearch 2.1.0 新增高性能 Rules 规则引擎插件,数据探索 Discover 等
数据库·产品
鹿角片ljp1 天前
苍穹外卖 day05:店铺营业状态设置与Redis入门实战
数据库·redis·缓存
m0_743470371 天前
使用Python进行PDF文件的处理与操作
jvm·数据库·python
Demon_Hao1 天前
JAVA通过Redis实现Key分区分片聚合点赞、收藏等计数同步数据库,并且通过布隆过滤器防重复点赞
java·数据库·redis
掘根1 天前
【微服务即时通讯】消息转发子服务
数据库·oracle
喜欢喝果茶.1 天前
SQL 预处理
数据库·sql
数据科学小丫1 天前
Python 数据存储操作_数据存储、补充知识点:Python 与 MySQL交互
数据库·python·mysql
Knight_AL1 天前
Nacos 启动问题 Failed to create database ’D:\nacos\nacos\data\derby-data’
开发语言·数据库·python