I was moving a Django app from “uWSGI running in a terminal” to a proper systemd service. The service would not start, kill did nothing useful, and later systemctl restart took a minute and a half. All three problems had the same cause: uWSGI does not treat SIGTERM as “stop”.
If you only want the fix, it is this line in your unit file:
KillSignal=SIGQUIT
The rest of this post explains what is going on, so you can recognise it next time.
The setup
A Django project with uWSGI installed in the project’s virtualenv, listening in HTTP mode on localhost. A web server (Apache or nginx) sits in front as a reverse proxy.
uwsgi.ini:
[uwsgi]
http = 127.0.0.1:8080
chdir = /srv/myproject
wsgi-file = myproject/wsgi.py
processes = 4
threads = 2
master = true
The first test was by hand, in the foreground:
(venv) $ uwsgi --ini /srv/myproject/uwsgi.ini
...
uWSGI http bound on 127.0.0.1:8080 fd 4
spawned uWSGI master process (pid: 6722)
spawned uWSGI worker 1 (pid: 6723, cores: 2)
...
It worked. Then I wrote the systemd unit.
Symptom 1: the service never comes up
/etc/systemd/system/myproject_uwsgi.service:
[Unit]
Description=uWSGI service for my Django project
After=network.target
[Service]
User=myuser
Group=myuser
WorkingDirectory=/srv/myproject
Environment="PATH=/srv/myproject/venv/bin"
ExecStart=/srv/myproject/venv/bin/uwsgi --ini /srv/myproject/uwsgi.ini
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
$ sudo systemctl daemon-reload
$ sudo systemctl enable --now myproject_uwsgi
$ sudo systemctl status myproject_uwsgi
● myproject_uwsgi.service - uWSGI service for my Django project
Loaded: loaded (/etc/systemd/system/myproject_uwsgi.service; enabled; ...)
Active: activating (auto-restart) (Result: exit-code) since ...
Main PID: 9564 (code=exited, status=1/FAILURE)
activating (auto-restart) plus status=1/FAILURE means the process starts, dies immediately, and Restart=always brings it back every 5 seconds, forever.
The reason was simple once I looked at the port:
$ ss -tlnp | grep 8080
LISTEN 0 100 127.0.0.1:8080 0.0.0.0:* users:(("uwsgi",pid=8220,fd=4),("uwsgi",pid=8212,fd=4))
The manual test instance was still running in another terminal and holding port 8080. The service could not bind to it. Fine, kill it.
Symptom 2: kill doesn’t kill it
$ kill 8212
$ ss -tlnp | grep 8080
LISTEN 0 100 127.0.0.1:8080 0.0.0.0:* users:(("uwsgi",pid=10255,fd=5),("uwsgi",pid=8212,fd=5))
Same master PID, still listening. In its terminal, uWSGI printed:
...gracefully (RE)spawned uWSGI master process (pid: 8212)
It did not die. It reloaded itself.
Why: uWSGI’s signal table
kill with no option sends SIGTERM. Almost every Unix daemon reads that as “shut down cleanly”. uWSGI does not. Its own documentation lists the signals like this:
| Signal | What uWSGI does |
|---|---|
| SIGHUP | graceful reload of workers and master |
| SIGTERM | “brutal” reload of workers and master |
| SIGINT | kill the whole stack immediately (this is Ctrl+C) |
| SIGQUIT | kill the whole stack immediately |
So the uWSGI docs themselves say: to shut it down, use SIGINT or SIGQUIT, or turn on the die-on-term option if you want SIGTERM to mean “stop”.
To get rid of the stray manual instance:
$ kill -INT 8212
$ ss -tlnp | grep 8080
$
Port free. The service started on the next restart attempt.
Symptom 3 (waiting to happen): systemctl stop hangs for 90 seconds
This is the part that would have bitten me later. When you run systemctl stop or systemctl restart, systemd sends SIGTERM by default to the service’s processes, waits for them to exit, and if they don’t, sends SIGKILL after the stop timeout (90 seconds by default).
Put that together with the table above:
systemctl restart myproject_uwsgi- systemd sends SIGTERM.
- uWSGI reloads instead of exiting.
- systemd waits… and waits… then SIGKILLs it after about 90 seconds.
People hit this regularly; there are bug reports of a uWSGI service taking exactly 1m30s to stop, ending in a journal line like State 'stop-sigterm' timed out. Killing.
The fix
Tell systemd which signal to use to stop the service. KillSignal= is the standard systemd option for that:
[Service]
...
Restart=always
RestartSec=5
KillSignal=SIGQUIT
$ sudo systemctl daemon-reload
$ sudo systemctl restart myproject_uwsgi
$ sudo systemctl status myproject_uwsgi
● myproject_uwsgi.service - uWSGI service for my Django project
Active: active (running) since ...; 2s ago
Main PID: 11439 (uwsgi)
Tasks: 10
CGroup: /system.slice/myproject_uwsgi.service
├─11439 /srv/myproject/venv/bin/uwsgi --ini /srv/myproject/uwsgi.ini
├─11459 ...
└─11466 ...
Now stop and restart are instant, and the master plus all workers are inside the service’s cgroup, so nothing is left behind.
SIGQUIT is also what the uWSGI documentation uses in its own systemd example. SIGINT works too.
Alternative: die-on-term
If you would rather keep systemd’s default SIGTERM (for example, because other tools such as docker stop also send SIGTERM), make uWSGI follow the convention instead:
[uwsgi]
...
die-on-term = true
Pick one approach. KillSignal=SIGQUIT is the smallest change if you only manage uWSGI through systemd.
Other things worth checking in the unit
- Use the venv’s uWSGI binary with its full path. systemd does not activate virtualenvs. Calling
/srv/myproject/venv/bin/uwsgiis what makes it load the Django from that venv. - Run it as a user that can read the project. Many examples use
User=www-data. If your project is inside a home directory withdrwx--x---permissions,www-datacannot even enter it, and with SQLite it also needs write access to the database file and its folder. - Don’t use
daemonizeinuwsgi.iniwhen systemd manages it; systemd expects the process to stay in the foreground.
Bonus: why your CSS is a 404 under uWSGI
Same project, different surprise: under runserver the stylesheet loaded, under uWSGI it was a 404, even with DEBUG = True:
$ curl -I http://127.0.0.1:8080/static/style.css
HTTP/1.1 404 Not Found
runserver includes a helper that serves /static/; the WSGI application that uWSGI runs does not. The fix is to collect static files into STATIC_ROOT and let something else serve them. Note STATIC_ROOT must be a different folder from anything in STATICFILES_DIRS:
# settings.py
STATIC_ROOT = BASE_DIR / "staticfiles"
(venv) $ python manage.py collectstatic
128 static files copied to '/srv/myproject/staticfiles'.
Then either let uWSGI map the URL to that folder:
static-map = /static=/srv/myproject/staticfiles
or, better in production, serve /static/ directly from nginx or Apache in front.
Summary
kill <pid>andsystemctl stopsend SIGTERM. uWSGI reloads on SIGTERM.- To stop it by hand:
kill -INT <pid>(or Ctrl+C). - In the systemd unit:
KillSignal=SIGQUIT, ordie-on-term = trueinuwsgi.ini. - If a new uWSGI service loops in
activating (auto-restart), checkss -tlnpfor an old instance holding the port.