即使已启用 Redis,它在 systemd 重启后也不会自动启动

即使已启用 Redis,它在 systemd 重启后也不会自动启动

我想在系统启动后使用 systemd 自动启动 redis 数据库。我在 ec2 上使用 x86 Ubuntu 时遇到了一些奇怪的问题:

systemd 声称该服务已启用。

ubuntu@:~$ systemctl status redis
● redis.service - Redis In-Memory Data Store
   Loaded: loaded (/etc/systemd/system/redis.service; enabled; vendor preset: enabled)
   Active: inactive (dead)

符号链接也存在:

root@ip-172-31-23-10:~# ls -l /etc/systemd/system/multi.target.wants/redis.service
lrwxrwxrwx 1 root root 33 Nov  5 10:25 /etc/systemd/system/multi.target.wants/redis.service -> /etc/systemd/system/redis.service
cat /etc/systemd/system/redis.service

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
Use=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi.target

重新启动机器后,我发现该服务尚未启动(systemctl status redis)并且没有尝试启动该服务的日志:

sudo journalctl -u redis
-- No entries --

如果我手动启动 redis - 一切正常

root@:~# systemctl start redis
root@:~# systemctl status redis
● redis.service - Redis In-Memory Data Store
   Loaded: loaded (/etc/systemd/system/redis.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2020-11-05 10:29:57 UTC; 4s ago
 Main PID: 2107 (redis-server)
    Tasks: 5
   Memory: 2.9M
      CPU: 13ms
   CGroup: /system.slice/redis.service
           └─2107 /usr/local/bin/redis-server 127.0.0.1:6379

Nov 05 10:29:57 ip- redis-server[2107]: 2107:M 05 Nov 2020 10:29:57.126 # WARNING: The TCP backlog setting of 511
Nov 05 10:29:57 ip- redis-server[2107]: 2107:M 05 Nov 2020 10:29:57.126 # Server initialized
Nov 05 10:29:57 ip- redis-server[2107]: 2107:M 05 Nov 2020 10:29:57.126 # WARNING overcommit_memory is set to 0! B
Nov 05 10:29:57 ip- redis-server[2107]: 2107:M 05 Nov 2020 10:29:57.131 * Loading RDB produced by version 6.0.8
Nov 05 10:29:57 ip redis-server[2107]: 2107:M 05 Nov 2020 10:29:57.131 * RDB age 717 seconds
Nov 05 10:29:57 ip redis-server[2107]: 2107:M 05 Nov 2020 10:29:57.131 * RDB memory usage when created 0.86 Mb
Nov 05 10:29:57 ip redis-server[2107]: 2107:M 05 Nov 2020 10:29:57.133 * DB loaded from disk: 0.006 seconds
Nov 05 10:29:57 ip redis-server[2107]: 2107:M 05 Nov 2020 10:29:57.133 * Ready to accept connections
Nov 05 10:29:57 ip redis-server[2107]: 2107:M 05 Nov 2020 10:29:57.133 # systemd supervision requested, but NOTIF
Nov 05 10:29:57 ip redis-server[2107]: 2107:M 05 Nov 2020 10:29:57.133 # systemd supervision requested, but NOTIF

答案1

我遇到了完全相同的问题。我尝试了很多方法,包括:

systemctl enable redis.service

是的,我知道它已经启用,但它没有自动启动。我收到错误,提示 systemctl 无法启用链接服务。这是因为该文件/etc/systemd/system/redis.service实际上是指向/lib/systemd/system/redis-server.service

我终于找到了答案,您必须针对文件的实际位置运行启用/禁用命令,即:

systemctl enable /lib/systemd/system/redis-server.service

完成后,我重新启动,然后 Redis 就开始运行了。

相关内容