Search code examples
environment-variablessystemd

Systemd not setting RUNTIME_DIRECTORY env when it should?


I have a pretty simple test service. Ultimately I'll be running a node daemon, but debugging this problem with env for now...

$ systemctl cat http-endpoints
# /etc/systemd/system/http-endpoints.service
[Unit]
Description=HTTP endpoints for triggering events
After=network.target

[Service]
Type=simple
;PassEnvironment=RUNTIME_DIRECTORY
RuntimeDirectory=endpoints
Environment=TEST=asdf
ExecStart=/usr/bin/env

[Install]
WantedBy=default.target

I'm trying to use the RuntimeDirectory= feature of systemd. I am expecting the environment variable RUNTIME_DIRECTORY.

It is not getting set.

systemd[1]: Started HTTP endpoints for triggering events.
env[26794]: LANG=en_US.UTF-8
env[26794]: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
env[26794]: INVOCATION_ID=fb24db240f454722b847ab7cefd5eb59
env[26794]: JOURNAL_STREAM=9:8063338
env[26794]: TEST=asdf

I have tried a few things.

If I manually set it, Environment=RUNTIME_DIRECTORY=/run/endpoints, it works. But that config should not be necessary.

The /run/endpoints directory is created as expected (and promptly deleted because env exits, but that's not the problem).

$ systemd --version
systemd 237
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN -PCRE2 default-hierarchy=hybrid

Solution

  • Ubuntu 18.04.3 does not have the lastest systemd.

    The support for the environment variables was added in v240.

    CHANGES WITH 240:
        * When the RuntimeDirectory=, StateDirectory=, CacheDirectory=,
          LogsDirectory=, ConfigurationDirectory= settings are used in a
          service the executed processes will now receive a set of environment
          variables containing the full paths of these directories.
          Specifically, RUNTIME_DIRECTORY=, STATE_DIRECTORY, CACHE_DIRECTORY,
          LOGS_DIRECTORY, CONFIGURATION_DIRECTORY are now set if these options
          are used. Note that these options may be used multiple times per
          service in which case the resulting paths will be concatenated and
          separated by colons.
    

    I hope this saves someone the time I spent trying to figure this out.