Search code examples
systemd

Is it possible the get the UID from within a systemd service file?


I have a service file which starts chromium on wayland:

[Unit]
Description=Launch Chromium
After=network.target weston.service
Requires=weston.service

[Service]
Type=simple
User=weston
Group=weston
Environment="XDG_RUNTIME_DIR=/run/user/1000"
Environment="WAYLAND_DISPLAY=wayland-1"
ExecStart=/usr/bin/chromium
TimeoutSec=infinity

[Install]
WantedBy=graphical.target

Normally the weston user gets assigned the UID 1000 (I'm building a Yocto image so the UID could change potentially if there were changes in the image). It would be nice if there was a way to get the UID from within the systemd service file so that the setting for XDG_RUNTIME_DIR variable is always valid.


Solution

  • Just set it to the first directory inside /run/user if you want to.

    ExecStart=/usr/bin/sh -c "XDG_RUNTIME_DIR=$(find /run/user/ -maxdepth 1 -mindepth 1 | head -n 1) /usr/bin/chromium"
    

    But instead, you should be running the service as a user service of the user that logs in inside the graphical console.