启用 systemctl 服务不会在启动时加载

启用 systemctl 服务不会在启动时加载

我已经设置了 gunicorn 来为两个 Django 站点提供服务,如下所示此设置。效果很好。如果我运行:

# systemctl start gunicorn@my_website.service

然后一切正常,我的网站可以按预期运行。因此,我决定在启动时启用此功能:

# systemctl enable gunicorn@my_website.service

让我们检查它是否已启用:

# systemctl is-enabled gunicorn@my_website.service
enabled

看起来不错。现在,让我们重新启动...

事实证明我的网站无法启动。nginx 可以正常工作,只是 gunicorn 似乎没有正常工作。让我们调查一下:

# systemctl status gunicorn@my_website.service
 ● gunicorn@my_website.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/[email protected]; indirect; vendor preset: enabled)
   Active: inactive (dead)

嗯...看起来很奇怪。检查启动后的日志:

# journalctl -u gunicorn@my_website.service -b
-- Logs begin at Mon 2019-04-08 06:04:03 UTC, end at Thu 2020-10-15 13:23:30 UTC. --
-- No entries --

非常令人困惑...甚至没有日志条目!当我手动启动服务时,我们恢复了运行:

# systemctl start gunicorn@my_website.service
# systemctl status gunicorn@my_website.service
● gunicorn@my_website.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/[email protected]; indirect; vendor preset: enabled)
   Active: active (running) since Thu 2020-10-15 13:25:29 UTC; 30s ago
 Main PID: 1272 (gunicorn)
    Tasks: 4 (limit: 1151)
   CGroup: /system.slice/system-gunicorn.slice/gunicorn@my_website.service
           ├─1272 /usr/bin/python3 /usr/local/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/my_website/gunicorn.sock my_website.wsgi:application
           ├─1294 /usr/bin/python3 /usr/local/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/my_website/gunicorn.sock my_website.wsgi:application
           ├─1297 /usr/bin/python3 /usr/local/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/my_website/gunicorn.sock my_website.wsgi:application
           └─1298 /usr/bin/python3 /usr/local/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/my_website/gunicorn.sock my_website.wsgi:application

Oct 15 13:25:29 web systemd[1]: Started gunicorn daemon.
Oct 15 13:25:29 web gunicorn[1272]: [2020-10-15 13:25:29 +0000] [1272] [INFO] Starting gunicorn 19.9.0
Oct 15 13:25:29 web gunicorn[1272]: [2020-10-15 13:25:29 +0000] [1272] [INFO] Listening at: unix:/home/my_website/gunicorn.sock (1272)
Oct 15 13:25:29 web gunicorn[1272]: [2020-10-15 13:25:29 +0000] [1272] [INFO] Using worker: sync
Oct 15 13:25:29 web gunicorn[1272]: [2020-10-15 13:25:29 +0000] [1294] [INFO] Booting worker with pid: 1294
Oct 15 13:25:29 web gunicorn[1272]: [2020-10-15 13:25:29 +0000] [1297] [INFO] Booting worker with pid: 1297
Oct 15 13:25:29 web gunicorn[1272]: [2020-10-15 13:25:29 +0000] [1298] [INFO] Booting worker with pid: 1298

确实,我的网站现在可以正常工作了!但是为什么启用此功能后似乎在启动时也无法运行?如何调试?

运行 Ubuntu 18.04。

根据要求,文件内容[email protected]如下:

# cat /etc/systemd/system/[email protected]
[Unit]
Description=gunicorn daemon
After=network.target
PartOf=gunicorn.target
# Since systemd 235 reloading target can pass through
ReloadPropagatedFrom=gunicorn.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/%i/
ExecStart=/usr/local/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/home/%i/gunicorn.sock \
          %i.wsgi:application

[Install]
WantedBy=gunicorn.target

答案1

我认为您还必须启用 gunicorn.target:

systemctl enable gunicorn.target

否则,由于没有服务或目标需要[电子邮件保护],它不会在启动时启动。

您可以通过检查哪些服务/目标依赖/需要此服务来调试此类问题,并要求反向依赖:

systemctl list-dependencies --reverse gunicorn.service

相关内容