Setting dynamic path in the redis.conf using the Environment variable

痴心易碎 提交于 2021-02-07 13:46:01

问题


I have a environment variable MY_HOME which has a path to a directory /home/abc

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/

like we do in command line, so that my config file picks the path based on the Environment variable.


回答1:


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}
...

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.




回答2:


i also find a solution,but redis config is no support for env var. i think have 2 method:

  1. start up redis by a script,script get env var and change redis config.
  2. start up redis by command line,and send env var as parameter.



回答3:


this is not supported by Redis, however it's achievable with the use of envsubst (which's installed by default on almost all modern distros) -to substitute in the values of environment variables before running 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



来源:https://stackoverflow.com/questions/19452637/setting-dynamic-path-in-the-redis-conf-using-the-environment-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!