使用bash脚本安装Redis,设置配置文件

use*_*631 2 bash redis 18.04

我想创建一个 bash 脚本,它会自动安装 redis:

我的问题是更改 2 个文件中的行:

#Install Redis
sudo apt install redis-server
sudo nano /etc/redis/redis.conf
Run Code Online (Sandbox Code Playgroud)

我需要找到一行并更改。默认情况下,受监督的指令设置为 no。

# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.    
supervised systemd # this line to change
Run Code Online (Sandbox Code Playgroud)
sudo systemctl reload redis.service
Run Code Online (Sandbox Code Playgroud)

  1. 须藤纳米 /etc/redis/redis.conf

    需要取消注释(删除 # 如果存在):

    bind 127.0.0.1 ::1
    
    Run Code Online (Sandbox Code Playgroud)

也可以测试一下吗?

redis-cli
Run Code Online (Sandbox Code Playgroud)

在随后的提示中,使用 ping 命令测试连通性:

Output
PONG
Run Code Online (Sandbox Code Playgroud)

或检查状态?

sudo systemctl status redis
Run Code Online (Sandbox Code Playgroud)

wal*_*tor 5

是的,这是可能的,但你的表现并不理想。这是一个批处理,所以不要使用nano,使用文本处理工具。不要在每个命令前加上sudo,而是将整个内容包装在一个脚本中,并使用它sudo来执行脚本。

类似的东西(“类似的东西”,我的意思是“我没有尝试过这个,也没有安装 redis-server。我认为这是我已经做过很多次的任务的另一个例子,但它应该可以工作”):

#!/bin/bash
if [[ $(id -u) != 0 ]] ; then
    echo "Must be run as root" >&2
    exit 1
fi
apt update
apt install redis-server
# Just in case, ...
systemctl stop redis-server
# Change "supervised no" so "supervised systemd"? Question is unclear
# If "#bind 127.0.0.1 ::1", change to "bind 127.0.0.1 ::1"
sed -e '/^supervised no/supervised systemd/' \
    -e 's/^# *bind 127\.0\.0\.1 ::1/bind 127.0.0.1 ::1' \
    /etc/redis/redis.conf >/etc/redis/redis.conf.new
# $(date +%y%b%d-%H%M%S) == "18Aug13-125913"
mv /etc/redis/redis.conf /etc/redis/redis.conf.$(date +%y%b%d-%H%M%S)
mv /etc/redis/redis.conf.new /etc/redis/redis.conf
systemctl start redis-server
# give redis-server a second to wake up
sleep 1
if [[ "$( echo 'ping' | /usr/bin/redis-cli )" == "PONG" ]] ; then
    echo "ping worked"
else
    echo "ping FAILED"
fi
systemctl status redis
systemctl status redis-server
exit 0
Run Code Online (Sandbox Code Playgroud)