ubuntu redis集群服务自启
一、自启脚本
redis_6379服务启动脚本
#!/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
PASSWORD=sa
EXEC=/opt/redis-cluster/6379/bin/redis-server
CLIEXEC=/opt/redis-cluster/6379/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/opt/redis-cluster/6379/bin/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 $PASSWORD -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
status)
PID=$(cat $PIDFILE)
if [ ! -x /proc/${PID} ]
then
echo 'Redis is not running'
else
echo "Redis is running ($PID)"
fi
;;
status)
$0 stop
$0 start
;;
*)
echo "Please use start or stop as first argument"
;;
esac
redis_6380服务启动脚本
#!/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_6380
# 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
PASSWORD=sa
EXEC=/opt/redis-cluster/6380/bin/redis-server
CLIEXEC=/opt/redis-cluster/6380/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/opt/redis-cluster/6380/bin/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 $PASSWORD -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
status)
PID=$(cat $PIDFILE)
if [ ! -x /proc/${PID} ]
then
echo 'Redis is not running'
else
echo "Redis is running ($PID)"
fi
;;
status)
$0 stop
$0 start
;;
*)
echo "Please use start or stop as first argument"
;;
esac
脚本可自行根据下图路径中的redis_init_script脚本修改
自启脚本拷贝至/etc/init.d目录下
# 移动自启脚本
sudo mv /home/Desktop/redis_6379 /etc/init.d/
sudo mv /home/Desktop/redis_6380 /etc/init.d/
二、redis.conf配置修改
redis配置要设置daemonize为yes 以及配置log文件路径 logfile(记得手动创建日志文件),使服务可以后台运行:
################################# GENERAL #####################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile "/usr/local/redis-5.0.10/log/redis-server6379.log"
三、执行开机自启设置命令
# 自启脚本添加可执行权限
sudo chmod +x /etc/init.d/redis_6379
sudo chmod +x /etc/init.d/redis_6380
# 更新启动项脚本
sudo update-rc.d redis_6379 defaults
sudo update-rc.d redis_6380 defaults
即可重启测试:
# 重启
reboot
redis服务自启成功
四、常用命令
# 启动Redis服务
service redis_6379 start
service redis_6380 start
# 关闭服务
service redis_6379 stop
service redis_6380 stop
# 重启服务
service redis_6379 restart
service redis_6380 restart
# 查看redis状态
service redis_6379 status
service redis_6380 status