繁体   English   中英

使用Environment变量在redis.conf中设置动态路径

[英]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种方法:

  1. 通过脚本启动redis,脚本获取env var并更改redis配置。
  2. 通过命令行启动redis,并将env var作为参数发送。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM