Redis源码包自带的服务启动脚本

时间:2022-04-24 17:38:33

       在维护Redis的过程中,启动和停止 服务是最基本的操作。


一、Redis服务的启动方式:

       # /opt/redis/sbin/redis-server  /data/redis/redis6379/6379.conf   

       默认端口是6379,如果端口换了,如:6380 

      # /opt/redis/sbin/redis-server -p 6380 /data/redis/redis6380/6380.conf


二、Redis服务的暂停方式:

       # /opt/redis/bin/redis-cli -p 6379 shutdown


在Redis源代码的utils目录下有一个 redis_init_script 初始化启动脚本。 

# cd /opt/webroot/soft/redis-3.2.1/utils

# cp redis_init_script /etc/init.d/redis_6379   

注意:redis_6379  服务名命名规则:redis_PORT,防止一台主机有多个端口号。

# vi /etc/init.d/redis_6379 

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

REDISPORT=6379
EXEC=/opt/redis/sbin/redis-server
CLIEXEC=/opt/redis/bin/redis-cli

PIDFILE=/var/run/${REDISPORT}.pid
CONF="/data/redis/redis6379/${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

修改对应的部分,然后设置开机启动,即可。