21

Does anyone have an example, or a link to an example of how to define a systemd .service which is activated by d-bus?

My understanding is that if I create a test.service file here:

/usr/share/dbus-1/services/test.service

With the following contents:

[D-BUS Service]
Name=org.me.test
Exec="/tmp/testamundo.sh"

Can the service now be started/stopped via d-bus calls to systemd.Manager? If so, how?

RandomUser
  • 4,140
  • 17
  • 58
  • 94

2 Answers2

25

Lets take a look at one of the services coming with systemd, hostnamed.

# cat /usr/share/dbus-1/system-services/org.freedesktop.hostname1.service

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[D-BUS Service]
Name=org.freedesktop.hostname1
Exec=/bin/false
User=root
SystemdService=dbus-org.freedesktop.hostname1.service

The magic is SystemdService= directive. The service specified with SystemdService= is what dbus-daemon asks systemd to activate.

We are expecting a service called dbus-org.freedesktop.hostname1.service in systemd service directory.

# readlink /usr/lib/systemd/system/dbus-org.freedesktop.hostname1.service
systemd-hostnamed.service

There you go, this way a dbus service org.freedesktop.hostname1.service tells systemd to activate a systemd service systemd-hostnamed.service.

And the systemd service looks like

# cat /usr/lib/systemd/system/systemd-hostnamed.service
...
...
[Service]
BusName=org.freedesktop.hostname1
...
...

The magic on the systemd service file is BusName= directive. This directive tells systemd to wait until the given bus name to appear on the bus before proceeding.

Note: A dbus service has completely different syntax than systemd service. You need both to be able to have a dbus activated daemon.

Umut
  • 2,317
  • 1
  • 17
  • 19
  • Thank-you for this explanation, this really cleared things up for me! – RandomUser Jul 31 '15 at 22:56
  • @Umut I have same kind of issue but in my case dbus activation should happen over session bus not the system, Can you please provide any guidance? – ASB Mar 17 '16 at 08:58
  • what happens when the `Exec` line isn't `/bin/false` but the same as `ExecStart` of the systemd service? – Fuseteam Jan 05 '23 at 11:10
3

To extend Umut's answer:

What is also in the systemd's service definition file is:

# cat /usr/lib/systemd/system/systemd-hostnamed.service
...
...
[Install]
Alias=dbus-org.freedesktop.hostname1.service
...
...

This makes sure the /usr/lib/systemd/system/dbus-org.freedesktop.hostname1.service symlink is installed when enabling the service.

The reason there the dbus service definition points to dbus-org.freedesktop.hostname1.service and not systemd-hostnamed.service is purely for convenience. This way it is clear that the hostnamed service is dbus activated. You could point directly to the actual service and skip the symlink and the line in the [Install] section

Hiram
  • 151
  • 3
  • 1
    At least on ArchLinux (systemd version `242.32`), this doesn't seem to be the case any more. There is no `[Install]` section in `systemd-hostnamed.service`, and instead `dbus-org.freedesktop.hostname1.service` is installed directly by the `systemd` package. – A.P. Jul 05 '19 at 10:56
  • Yes you don't need the `[Install]` section according to this [page](https://www.freedesktop.org/software/systemd/man/systemd.service.html#Examples). See Example 6 – smac89 Oct 19 '20 at 19:31