1. 程式人生 > >centos下為redis新增service

centos下為redis新增service

為了方便地重啟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進行快速啟動停止了。