top | item 46870844

(no title)

xerxes901 | 27 days ago

> As a trivial example, you can dynamically depend on other services depending on system configuration (as PostgreSQL does)

Depending on what you want to do, a generator might be appropriate:

> Their main purpose is to convert configuration and execution context parameters that are not native to the service manager into dynamically generated unit files, symlinks or unit file drop-ins

discuss

order

simoncion|26 days ago

Well, here are the relevant parts of the service file:

  get_config() {
      [ -f "${PGDATA%/}/postgresql.conf" ] || return 1
  
      eval echo $(sed -e 's:#.*::' "${PGDATA%/}/postgresql.conf" \
          | awk '$1 == "'$1'" { print ($2 == "=" ? $3 : $2) }')
  }
  
  depend() {
      use net
      provide postgresql
  
      if [ "$(get_config log_destination)" = "syslog" ]; then
          use logger
      fi
  }
If PostgreSQL has been configured, this reads its config file, looks to see if it's configured to use 'syslog' as its log destination, and -if so- adds a dependency on the 'logger' "meta-service". [0]

What would this look like with a systemd service file generator?

[0] What's a "meta-service"? 'provide postgresql' makes the service started by this service file provide the 'postgresql' "meta-service". This is useful for PostgreSQL because you can install multiple versions of the software simultaneously... so the service files are named like postgresql-17, and postgresql-18. The 'logger' "meta-service" is useful because who cares which syslog software you have installed... you only care that it speaks syslog.

xerxes901|26 days ago

Yeah parsing config files with regular expressions that may or may not properly handle quoting or line continuations etc is… not a great idea in my opinion.

But of course in this particular case, because systemd makes the /dev/log journal/syslog socket a dependency of every unit by default, there is no need to encode this dependency at all.

Anyway if you really wanted to you could write this script as a generator and have it put a drop-in in /run/systemd/system/postgres.service.d. But… why?