8

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.

Shubham
  • 2,847
  • 4
  • 24
  • 37
  • Looks like there is no support for environment variables in Redis configuration (at least not in 2.6.16), unfortunately. – BorisOkunskiy Nov 06 '13 at 06:23

3 Answers3

3

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.

dg99
  • 5,456
  • 3
  • 37
  • 49
1

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

Wis
  • 484
  • 7
  • 22
0

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.
jolestar
  • 1,285
  • 15
  • 21