Gunicorn
啟用 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/gunicorn@.service; 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/gunicorn@.service; 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。
根據要求,
gunicorn@.service
文件內容如下:# cat /etc/systemd/system/gunicorn@.service [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
我認為您還必須啟用 gunicorn.target:
systemctl enable gunicorn.target
否則,由於沒有需要 gunicorn@.service 的服務或目標,它不會在啟動時啟動。
您可以調試此類問題,檢查哪些服務/目標依賴/要求此服務要求反向依賴:
systemctl list-dependencies --reverse gunicorn.service