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

Abi*_*lah 6 configuration environment-variables redis

我有一个环境变量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/
Run Code Online (Sandbox Code Playgroud)

就像我们在命令行中那样,以便我的配置文件根据Environment变量选择路径.

dg9*_*g99 3

因为 Redis 可以从 读取其配置stdin,所以我做了一些与 @jolestar 建议的非常相似的操作。我将占位符变量放入我的变量中redis.conf,然后sed在 Redis 启动器中使用它们进行替换。例如:

==========
$MY_HOME/redis/redis.conf
==========
...
pidfile {DIR}/pids/server{n}.pid
port 123{n}
...
Run Code Online (Sandbox Code Playgroud)

然后我有一个启动 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
Run Code Online (Sandbox Code Playgroud)

我一直在使用这种方法并且效果很好。