0

I have set up gunicorn to server two Django sites, as per this setup. This works well. If I run:

# systemctl start gunicorn@my_website.service

Then all is well and my website is being served as expected. So I move to enable this on boot:

# systemctl enable gunicorn@my_website.service

Let's check it's enabled:

# systemctl is-enabled gunicorn@my_website.service
enabled

Looks good. Now, let's reboot...

Turns out my site is not up. nginx is working, it just seems that gunicorn isn't doing it's thing. Let's investigate:

# 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)

Hmmm... that looks weird. Checking out the log since boot:

# 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 --

Very puzzling... not even a log entry! When I start the service manually we're back in operations:

# 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

And indeed, my website works now! But why is this seemingly not even run on boot when it's enabled? How to debug this?

Running Ubuntu 18.04.

As requested, the content of the gunicorn@.service file below:

# 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

1 Answers1

1

I think you have to enable also the gunicorn.target:

systemctl enable gunicorn.target

Otherwise, as there is no service nor target which requires the gunicorn@.service, it won't be started on boot.

You can debug this kind of problems checking which services/targets depends/require this service asking for reverse dependencies:

systemctl list-dependencies --reverse gunicorn.service
Jesús Ángel
  • 518
  • 2
  • 6
  • Fantastic, that was indeed the problem it seems! It now works! –  Oct 18 '20 at 05:03
  • I will add the bounty but can't do it yet apparently, it needs more time. –  Oct 18 '20 at 05:03
  • Do note that the list-dependencies command didn't work: `Failed to get properties of gunicorn@.service: Unit name gunicorn@.service is neither a valid> gunicorn@.service` but the actual booting does work well now. –  Oct 18 '20 at 12:36
  • Probably you have to use the complete service name: `systemctl list-dependencies --reverse gunicorn@my_website.service` or just `systemctl list-dependencies --reverse gunicorn.service` – Jesús Ángel Oct 18 '20 at 12:42
  • Ah great, yes that did the trick! Thanks @Jesús! –  Oct 18 '20 at 15:14