"Process org.freedesktop.systemd1 exited with status 1" when using GetUnitPropertiesContext
Closed this issue · 1 comments
I am using this library in my program, and when I manually start the program, everything is fine. The programm will call dbus.NewUserConnectionContext()
to establish a new dbus connection, and afterwards it will call GetUnitPropertiesContext()
to retrieve the status of a given systemd unit. This works well.
However, I also put this program in a dedicate systemd unit to start it upon boot. In this case, after reboot of the machine, the above does no longer work. Instead, GetUnitPropertiesContext()
will give me the error: Process org.freedesktop.systemd1 exited with status 1
Since the status query is triggered via HTTP at regular intervals, my program will retry this every couple of seconds, but it will fail all the time.
However, when I do a systemctl restart myprogram.service
, it will instantly be able to query the status using the above mentioned method.
I don't quite understand what is going on here. On the one hand, I assume it has to do with systemd dependencies, and I should put dependencies to dbus/systemd to myprogram.service
- which I have tried, but did not help. On the other hand, even if this was a race condition during boot, I do not understand why a simple restart of my program solves the issue. Does dbus.NewUserConnectionContext()
create some shared/global context which does not reset after conn.Close()
and thus persists the observer error until my program restarts? It does not make sense to me.
This is the code which is called every 5 seconds:
func (s *SystemdPlatform) HandleStatus(ctx context.Context, unitName string) ([]byte, error) {
conn, err := dbus.NewUserConnectionContext(ctx)
if err != nil {
log.Error().Msgf("Failed to connect to DBUS: %s", err)
return nil, err
}
defer conn.Close()
props, err := conn.GetUnitPropertiesContext(ctx, unitName)
if err != nil {
log.Error().Msgf("Failed to retrieve systemd status for service '%s': %s", unitName, err)
return nil, err
}
[...]
}
Seems like a user instance of systemd is not yet spawned and this leads to the program not being able to query status right after boot.