gosystemddbusautostart

How to set dbus/systemd as dependency in systemd unit


I have a small program written in Go which uses the go-systemd library to interact with systemd using dbus.

The program itself is registered as a systemd unit and shall start upon boot. When the program is running, it will regularly query systemd unit status for several units.

Now when I manually start the program, everything is fine. However, when started after reboot by systemd, it will "hang", means it will be unable to query systemd unit status via dbus. I will get the following message from the go-systemd library:

Process org.freedesktop.systemd1 exited with status 1

Now when I just restart my program, it will immediately be able to query systemd units without error. This leads me to the assumption of wrong/misconfigured dependencies for my program, so that upon boot there will be a race condition which leads to the program being unable to communicate with systemd over dbus.

Now I have already put this, and several other combinations, however it does not help:

Requires=dbus.service
After=dbus.service
Wants=org.freedesktop.systemd1

How do I need to configure my own systemd unit (of the go-program) in order for it to be able to communicate with systemd over dbus right after reboot? What dependency do I have to configure?


Solution

  • It sounds like your program is trying to connect to the DBus Session bus by default, since you say it works fine when you run it manually. When an application starts up from Systemd, it will by default running as root with no session bus to connect to, so it will fail.

    The session bus is generally started on a per-login basis when a user logs in on a graphical terminal. It does not start by default when logging in on a terminal.

    If this is the case, you have two options that I can see at the moment:

    1. Use the system bus instead of the session bus.
    2. Create your own bus that you connect to, different from the session bus and the system bus. The simplest way is to use dbus-launch to create a new bus that your application can connect to.