Search code examples
configurationredisenvironment-variables

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


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.


Solution

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