[systemd-devel] Best practices for lots of similar units?

Roman Odaisky roma at qwertty.com
Thu Jul 23 21:12:20 UTC 2020


Hi All,

Suppose I want to run a number of similar services that all require the same 
boilerplate. How to avoid repeating myself when creating unit files?

Let’s take a concrete example, websites. The boilerplate is as follows:

[Unit]
After=syslog.target

[Service]
ExecStart=/usr/bin/uwsgi \
    --socket /run/uwsgi/USER/CODENAME.sock \
    --plugin python3 \
    --plugin logfile \
    --logto /srv/CODENAME/logs/uwsgi.log \
    --logfile-chmod 644 \
    --virtualenv /srv/CODENAME/venv \
    --pythonpath /srv/CODENAME/app \
    --chdir /srv/CODENAME/app \
    --env DJANGO_SETTINGS_MODULE=settings \
    --module django.core.wsgi:get_wsgi_application() \
    --procname-prefix "CODENAME " \
    --ini /etc/uwsgi/common.ini

User=USER
Restart=always
Type=notify
NotifyAccess=all
StandardError=syslog

This requires two parameters, CODENAME and USER. Were the websites a bit less 
homogeneous, I could easily see them requiring more parameters. But let’s 
start with two.

I created /etc/systemd/system/wsgi-.service.d/base.conf with the above 
content, and I’m using %i for codename and %j for user. This lets me run 
services as wsgi-USER at CODENAME.{service,socket} successfully, but has these 
problems:

 - Does not allow me to specify common [Install] directives (necessary for 
.socket units) because systemd does not allow that in drop-ins
 - Does not allow me to specify more than two parameters %i and %j
 - Requires me to pre-create wsgi-USER at .{service,socket} files while taking
   care for them not to be empty, lest systemd think they’re masked
 - Is in general very hacky and unwieldy.

What good solution exists for this use case?

-- 
WBR
Roman.




More information about the systemd-devel mailing list