Systemd

無法在 Ubuntu Server 20.04.2 上將 Redis 初始化為 systemd 服務(受監督的 systemd)

  • February 8, 2022

supervised systemd在 Ubuntu Server 20.04 上,Redis (5.0.7) 設置為 on後不會啟動/etc/redis/redis.conf,但使用 . 手動執行時不會列印錯誤/usr/bin/redis-server /etc/redis/redis.conf

此外,當手動執行時,可能會發現它正在偵聽 6379 埠netstat -tulpn

重現

作為根:

  • 更新軟體包並安裝 Redisapt update && apt install redis-server
  • /etc/redis/redis.conf,將行更改supervised nosupervised systemd
  • 嘗試使用service redis-server restartor重啟 Redissystemctl restart redis

上述命令的輸出是:

Job for redis-server.service failed because the control process exited with error code.
See "systemctl status redis-server.service" and "journalctl -xe" for details.

的輸出systemctl status redis-server.service是:

● redis-server.service - Advanced key-value store
    Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
    Active: failed (Result: exit-code) since Wed 2021-02-03 12:15:56 -03; 28s ago
      Docs: http://redis.io/documentation,
            man:redis-server(1)
   Process: 3851 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=1/FAILURE)

Feb 03 12:15:56 mywebsite.com systemd[1]: redis-server.service: Scheduled restart job, restart counter is at 5.
Feb 03 12:15:56 mywebsite.com systemd[1]: Stopped Advanced key-value store.
Feb 03 12:15:56 mywebsite.com systemd[1]: redis-server.service: Start request repeated too quickly.
Feb 03 12:15:56 mywebsite.com systemd[1]: redis-server.service: Failed with result 'exit-code'.
Feb 03 12:15:56 mywebsite.com systemd[1]: Failed to start Advanced key-value store.

嘗試重新啟動 Redis 服務時不會列印任何日誌/var/log/redis/redis-server.log,但手動執行時會列印以下文本:

4231:C 03 Feb 2021 12:27:52.053 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4231:C 03 Feb 2021 12:27:52.053 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=4231, just started
4231:C 03 Feb 2021 12:27:52.053 # Configuration loaded
4232:M 03 Feb 2021 12:27:52.063 * Increased maximum number of open files to 10032 (it was originally set to 1024).
               _._
          _.-``__ ''-._
     _.-``    `.  `_.  ''-._           Redis 5.0.7 (00000000/0) 64 bit
 .-`` .-```.  ```\/    _.,_ ''-._
(    '      ,       .-`  | `,    )     Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
|    `-._   `._    /     _.-'    |     PID: 4232
 `-._    `-._  `-./  _.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._        _.-'_.-'    |           http://redis.io
 `-._    `-._`-.__.-'_.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._        _.-'_.-'    |
 `-._    `-._`-.__.-'_.-'    _.-'
     `-._    `-.__.-'    _.-'
         `-._        _.-'
             `-.__.-'

4232:M 03 Feb 2021 12:27:52.065 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4232:M 03 Feb 2021 12:27:52.065 # Server initialized
4232:M 03 Feb 2021 12:27:52.066 * Ready to accept connections

軟體版本:

Redis 伺服器 v=5.0.7 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=636cde3b5c7a3923

Ubuntu 20.04.2 LTS - 64 位

系統 245 (245.4-4ubuntu3.4)

這很奇怪,但是 Ubuntu 安裝 redis-server並沒有使用 redis 與 systemd 通信的能力。在這個平台上,你應該保留 redis.conf 的設置supervised no


如果您出於某種原因確實想啟用此功能,則必須自定義 systemd 單元,例如:

ubuntu@vmtest-ubuntu2004:~$ sudo systemctl edit redis-server.service

在 nano 中,您應該只添加以下內容:

[Service]
Type=notify

然後告訴 systemd 載入更改。

ubuntu@vmtest-ubuntu2004:~$ sudo systemctl daemon-reload

現在你需要編輯 redis.conf:

ubuntu@vmtest-ubuntu2004:~$ sudo nano /etc/redis/redis.conf

並進行以下更改:

daemonize no
supervised systemd

現在您可以重新啟動 redis-server:

ubuntu@vmtest-ubuntu2004:~$ sudo systemctl start redis-server
ubuntu@vmtest-ubuntu2004:~$ sudo systemctl status redis-server
* redis-server.service - Advanced key-value store
    Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
   Drop-In: /etc/systemd/system/redis-server.service.d
            `-override.conf
    Active: active (running) since Wed 2021-02-03 11:55:09 EST; 1min 8s ago
      Docs: http://redis.io/documentation,
            man:redis-server(1)
  Main PID: 1768 (redis-server)
     Tasks: 4 (limit: 2318)
    Memory: 1.8M
    CGroup: /system.slice/redis-server.service
            `-1768 /usr/bin/redis-server 127.0.0.1:6379

Feb 03 11:55:09 vmtest-ubuntu2004 systemd[1]: Starting Advanced key-value store...
Feb 03 11:55:09 vmtest-ubuntu2004 systemd[1]: Started Advanced key-value store.

引用自:https://serverfault.com/questions/1052285