[英]Setting dynamic path in the redis.conf using the Environment variable
我有一个环境变量MY_HOME
,它有一个目录/home/abc
的路径
现在,我有一个redis.conf
文件,我需要像这样设置这个路径
**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变量选择路径。
因为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
我一直在使用这种方法,效果很好。
Redis不支持这一点,但是使用envsubst
(几乎所有现代发行版都默认安装)都可以实现 - 在运行redis-server
之前替换环境变量的值。
envsubst '$HOME:$MY_HOME' < ~/.tmpl_redis.conf > ~/.redis.conf && redis-server ~/.redis.conf
# or
envsubst '$MY_HOME' < ~/.tmpl_redis.conf > ~/.redis.conf && redis-server !#:5 # 5th argument from the previous command
我也找到了解决方案,但redis config不支持env var。 我认为有2种方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.