2013-10-18 210 views
5

我有一個環境變量MY_HOME這也在目錄的路徑redis.conf設置動態路徑「的/ home/ABC」在使用環境變量

現在,我有一個redis.conf文件,在其中我需要設置這個路徑是這樣

redis.conf

$了pidfile MY_HOME /本地的/ var /的PID/redis.pid

日誌文件$ MY_HOME /本地的/ var /日誌/ redis.log

dir $ MY_HOME/local/var/lib/redis/

就像我們在命令行中那樣,以便我的配置文件根據環境變量選擇路徑。

+0

不幸的是,在Redis配置中看起來不支持環境變量(至少在2.6.16中不支持)。 – incarnate

回答

-1

我也找到一個解決方案,但redis配置不支持env var。 我認爲有2種方法:

  1. 通過腳本啓動redis,腳本獲取env var並更改redis配置。
  2. 通過命令行啓動redis,併發送env var作爲參數。
2

因爲Redis可以從stdin讀取它的配置,所以我執行的操作與@jolestar的建議非常相似。我在我的redis.conf中放置了佔位符變量,然後在我的Redis啓動器中使用sed替換它們。例如:

========== 
$MY_HOME/redis/redis.conf 
========== 
... 
pidfile {DIR}/pids/server{n}.pid 
port 123{n} 
... 

然後,我有一個腳本來啓動的Redis:

========== 
runredis.sh 
========== 
DIR=$MY_HOME/redis 
for n in {1..4}; do 
    echo "starting redis-server #$n ..." 
    sed -e "s/{n}/$n/g" -e "s/{DIR}/$DIR/g" < $DIR/redis.conf | redis-server - 
done 

我一直在使用這種方法永遠和它的作品了好。