1. 程式人生 > >Redis 在Centos7下配置開機自啟動

Redis 在Centos7下配置開機自啟動

腳本 fin sta ive 鏈接 權限 開機自啟 chmod usr

設置Redis開機啟動需要如下幾個步驟:

  1. 編寫配置腳本 [ vim /etc/init.d/redis ]
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
#chkconfig: 2345 80 90
#description:auto_run
REDISPORT=6379
EXEC=/usr/local/bin/redis/src/redis-server
CLIEXEC=/usr/local/bin/redis/src/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/bin/redis/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 -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
  1. 修改redis.conf,打開後臺運行選項
# 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
  1. 修改文件執行權限
chmod +x /etc/init.d/redis
  1. 設置開機啟動
# 嘗試啟動或停止 redis
service redis start
service redis stop
# 開啟服務自啟動
chkconfig redis on

5.異常處理
A. 執行 [ service redis start ] 提示服務不支持 chkconfig,在開機腳本前添加如下內容:

#chkconfig: 2345 80 90
#description:auto_run

B. 如果在Windows下編輯的開機腳本,由於Windows中的換行符為CRLF, 而Unix(或Linux)換行符為LF,會導致開機腳本執行報錯,把腳本通過notepad++轉化為Unix格式。



作者:_LiuWei
鏈接:http://www.jianshu.com/p/20b761ae993c
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請註明出處。

Redis 在Centos7下配置開機自啟動