如水,总是无言。若你安好,便是晴天。时光

centos7 SystemV 开机自启动脚本配置方法 redis集群三主三从

1、安装redis集群

参考: redis三主三从集群安装

2、编写redis启动脚本

2.1、建立启动脚本

https://github.com/redis/redis/blob/unstable/utils/redis_init_script
vi /etc/init.d/redis_init_script 把下面代码粘贴到文件里保存

#!/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

改造下面参数

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"
$EXEC $CONF

改造后示例

REDISPORT=7000
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis/redis_${REDISPORT}.pid
CONF="/usr/local/redis/conf/${REDISPORT}.conf"
nohup $EXEC $CONF &

改造后脚本示例

#!/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=7000
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis/redis_${REDISPORT}.pid
CONF="/usr/local/redis/conf/${REDISPORT}.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
nohup $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

2.2、复制多份redis启动脚本给集群使用

cd /etc/init.d
cp redis_init_script redis_init_script_6379
cp redis_init_script redis_init_script_6380

[root@localhost init.d]# cd /etc/init.d/
[root@localhost init.d]# ls -l
total 44
-rw-r--r--. 1 root root 18281 Aug 24 2018 functions
-rwxr-xr-x. 1 root root 4569 Aug 24 2018 netconsole
-rwxr-xr-x. 1 root root 7923 Aug 24 2018 network
-rw-r--r--. 1 root root 1160 Oct 31 2018 README
-rwxr-xr-x. 1 root root 1 Mar 31 15:19 redis_init_script_6379
-rwxr-xr-x. 1 root root 0 Mar 31 15:19 redis_init_script_6380
[root@localhost init.d]#

注意:
CONF=“/etc/redis/${REDISPORT}.conf”
在/etc/redis下相对应准备多份配置文件给集群使用

[root@localhost redis]# cd /etc/redis/
[root@localhost redis]# ls -l 63*
-rw-r--r--. 1 root root 0 Mar 31 15:16 6379.conf
-rw-r--r--. 1 root root 0 Mar 31 15:16 6380.conf
[root@localhost redis]#

2.3、添加可执行权限

chmod +x /etc/init.d/redis_init_script_6379
chmod +x /etc/init.d/redis_init_script_6380

3、配置开机自启动

添加开机自启动
chkconfig --add redis_init_script_6379
chkconfig --add redis_init_script_6380

[root@localhost init.d]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost init.d]# chkconfig --add redis_init_script_63
redis_init_script_6379 redis_init_script_6380
[root@localhost init.d]# chkconfig --add redis_init_script_6379
[root@localhost init.d]# chkconfig --add redis_init_script_6380
[root@localhost init.d]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
redis_init_script_6379 0:off 1:off 2:on 3:on 4:on 5:on 6:off
redis_init_script_6380 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost init.d]#

查看开机自启动配置
默认开启runlevel=2345的启动
在这里插入图片描述

posted @   OceanWaves  阅读(166)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
/* 看板娘 */ /* 粒子吸附*/
点击右上角即可分享
微信分享提示

目录导航