systemd: How to wait for a device before starting a service

Lennart Poettering mzerqung at 0pointer.de
Fri Jan 6 23:06:49 UTC 2012


On Fri, 06.01.12 14:55, Richard Shaw (hobbes1069 at gmail.com) wrote:

> Ok, I didn't know how to make the subject any shorter, but there's a
> big BUT in this, but (hehe) first a summary.
> 
> I have a user of MythTV that has capture devices which require a
> firmware be uploaded. As a consequence, the /dev paths are not always
> created by the time mythbackend tries to start. A solution to this is
> to create a mythbackend.path unit file which will wait for the devices
> to be created, BUT, they occasionally fail to initialize and the path
> unit file doesn't seem to have a timeout option (or option for what to
> do once the timeout is reached).
> 
> A partial solution would be to use a timer unit file, but it too
> doesn't do quite what I need. It doesn't appear that I can wait for
> the start of one unit file (mythbackend.path) while starting a
> different unit file (mythbackend.service) once the timeout is reached.
> 
> I could use the same 'After=...' as I have on the main service file
> which would approximate the same startup time, but that's rather
> hackish. I am assuming that having two unit files (a path and a timer)
> trying to start the same service isn't an issue and that the first one
> wins.
> 
> Below is the entire message I was sent with his solution which is even
> more complicated than what I'm proposing, but appears to work.

Drop a udev rules file into /lib/udev/rules.d (if this is for a package)
or /etc/udev/rules.d (if this is just a local installation) where you
match the device and add the systemd tag to it:

SUBSYSTEM=="foobar", SOMEMOREMATCHESHERE, TAG+="systemd"

(Replace foobar with the subsystem name of your device and
SOMEMOREMATCHESHERE with some sensible matches that identify your device
uniquely).

That will result in a .device unit to show up in systemd whenever the
device is available, named after the /dev path (or /sys path if it
doesn't have a device node).

Then, you can add the following to your service:

Wants=dev-waldo.device
After=dev-waldo.device

(Assuming your device is called /dev/waldo)

And that should do the job.

Lennart

-- 
Lennart Poettering - Red Hat, Inc.


More information about the devel mailing list