天天看點

linux中redis開機啟動腳本,CentOS 6.5安裝Redis及開機啟動腳本

一:CentOS 6.5下載下傳安裝Redis

1、下載下傳源碼,解壓縮後編譯源碼

# wget http://download.redis.io/releases/redis-2.8.3.tar.gz

# tar xzf redis-2.8.3.tar.gz

# cd redis-2.8.3

# make

2、進入安裝目錄的src檔案夾下,有四個可執行檔案redis-server、redis-benchmark、redis-cli和redis.conf,複制到同一個目錄下

# mkdir /usr/redis

# cp redis-server  /usr/redis

# cp redis-benchmark /usr/redis

# cp redis-cli  /usr/redis

# cp redis.conf  /usr/redis

# cd /usr/redis

3、啟動Redis服務。

# cd /usr/redis

# ./redis-server redis.conf

啟動異常:

情況一:

[17496] 08 Oct 11:48:09.153 # Server started, Redis version 2.8.17

[17496] 08 Oct 11:48:09.153 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

[17496] 08 Oct 11:48:09.153 * The server is now ready to accept connections on port 6379

解決辦法:編輯 /etc/sysctl.conf , 添加一項 vm.overcommit_memory = 1,重新開機生效。

4、用戶端測試。

redis 127.0.0.1:6379>  #顯示此行意味着安裝成功。

二:設定redis開機啟動

環境:Linux系統為CentOS 6.6

1.編寫啟動腳本

注意:預設的redis.conf檔案參數是前台啟動的,修改daemonize no為daemonize yes則為背景啟動。

腳本的編碼格式在Windows上編碼放在Linux可能不識别,可以用UltraEdit轉換下格式“檔案-->轉換-->DOS 轉 UNIX“

#!/bin/sh

#chkconfig: 345 86 14

#description: Startup and shutdown script for Redis

PROGDIR=/usr/redis #安裝路徑

PROGNAME=redis-server

DAEMON=$PROGDIR/$PROGNAME

CONFIG=/usr/redis/redis.conf

PIDFILE=/var/run/redis.pid

DESC="redis daemon"

SCRIPTNAME=/etc/rc.d/init.d/redis

start()

{

if test -x $DAEMON

then

echo -e "Starting $DESC: $PROGNAME"

if $DAEMON $CONFIG

then

echo -e "OK"

else

echo -e "failed"

fi

else

echo -e "Couldn't find Redis Server ($DAEMON)"

fi

}

stop()

{

if test -e $PIDFILE

then

echo -e "Stopping $DESC: $PROGNAME"

if kill `cat $PIDFILE`

then

echo -e "OK"

else

echo -e "failed"

fi

else

echo -e "No Redis Server ($DAEMON) running"

fi

}

restart()

{

echo -e "Restarting $DESC: $PROGNAME"

stop

start

}

list()

{

ps aux | grep $PROGNAME

}

case $1 in

start)

start

;;

stop)

stop

;;

restart)

restart

;;

list)

list

;;

*)

echo "Usage: $SCRIPTNAME {start|stop|restart|list}" >&2

exit 1

;;

esac

exit 0

把redis腳本檔案放在 /etc/rc.d/init.d/ 目錄下

2、增加服務并開機啟動

# chmod +x /etc/rc.d/init.d/redis

# chkconfig --add redis

# chkconfig --level 345 redis on

# chkconfig --list redis

3、重新開機測試。

下面關于Redis的文章您也可能喜歡,不妨參考下:

Redis 的詳細介紹:請點這裡

Redis 的下載下傳位址:請點這裡

linux中redis開機啟動腳本,CentOS 6.5安裝Redis及開機啟動腳本