centos下为redis添加service

来源:互联网 发布:共享文件夹监控软件 编辑:程序博客网 时间:2024/06/04 23:24

为了方便地重启redis服务 以下为配置service快速启动或停止redis服务的步骤

第一步:首先 进入redis安装目录中的utils中

第二步:将其中的redis_init_script文件复制到/etc/init.d下


第三步:进入/etc/init.d 将redis_init_script更名为redis

第四步:命令:mv redis_init_script redis

第五步:编辑redis

vim /etc/init.d/redis

进入后编辑这些为你的redis安装目录



修改后的如下

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


REDISPORT=6379
EXEC=/var/www/redis-stable/src/redis-server
CLIEXEC=/var/www/redis-stable/src/redis-cli


PIDFILE=/var/redis/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 -a "root" -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    restart)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -a "root" -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                        echo "Restarting Redis server..."
                done
                $EXEC $CONF


PIDFILE=/var/redis/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 -a "root" -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    restart)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -a "root" -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                        echo "Restarting Redis server..."
                done
                $EXEC $CONF
                echo "Redis restart success!"
        fi
        ;;
    *)

        echo "Please use start or stop as first argument"
        ;;
esac


结束:这样就可以在用service redis stop、service redis start对redis进行快速启动停止了。