使用环境变量在redis.conf中设置动态路径 [英] Setting dynamic path in the redis.conf using the Environment variable
问题描述
我有一个环境变量 MY_HOME
,该变量具有目录/home/abc
I have a environment variable MY_HOME
which has a path to a directory /home/abc
现在,我有一个 redis.conf
文件,我需要在其中设置此路径
Now, I have a redis.conf
file In which I need to set this path like this
**redis.conf**
pidfile $MY_HOME/local/var/pids/redis.pid
logfile $MY_HOME/local/var/log/redis.log
dir $MY_HOME/local/var/lib/redis/
就像我们在命令行中所做的那样,以便我的配置文件根据Environment变量选择路径.
like we do in command line, so that my config file picks the path based on the Environment variable.
推荐答案
因为Redis可以从 stdin
中读取其配置,所以我做的事情与@jolestar建议的非常相似.我将占位符变量放入 redis.conf
中,然后在Redis启动器中使用 sed
替换了它们.例如:
Because Redis can read its config from stdin
, I do something very similar to what @jolestar suggested. I put placeholder variables in my redis.conf
and then replace them using sed
in my Redis launcher. For example:
==========
$MY_HOME/redis/redis.conf
==========
...
pidfile {DIR}/pids/server{n}.pid
port 123{n}
...
然后我有一个脚本来启动Redis:
Then I have a script to start 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
我一直在使用这种方法,效果很好.
I've been using this approach forever and it works out well.
这篇关于使用环境变量在redis.conf中设置动态路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!