RandomUser
RandomUser

Reputation: 4220

How to define a d-bus activated systemd service?

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?

Upvotes: 21

Views: 26128

Answers (2)

Hiram
Hiram

Reputation: 171

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

Upvotes: 3

Umut
Umut

Reputation: 2493

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.

Upvotes: 25

Related Questions