Quite a few people over the years have run into the problem, what to do when a service requires a device, but the service tries to go online before the device does?
One of the most common cases is if you have something like a display device driven by USB serial. That is, “lcddisplay.service” needs /dev/ttyUSB0.
Search the web for that and you’ll generally see uncomfortable hacks involving udev, but actually it can be much easier than that.
systemd resource control subsystem for Linux can be configured to wait for filesystem resources using the resource pathname. So, for example, if I have a systemd mount for /export/usernet/, and I want to do things with files in that directory, I can set up the dependent service to be After=export-usernet.device”. Note that the “device” path is always absolute and that the slashes in the path are replaced with dashes, because otherwise the systemd resource pathname would appear to be a directory subtree. Incidentally, names with dashes in them double the dashes to avoid confusion with a slash-turned-dash.
However, things like /dev/ttyUSB0 are a little different, as they are managed not by the filesystem per se, but by
udev – the hotswap manager. So does this trick still work?
Yes, it does! simply make your dependency on
dev-ttyUSB0.device and you’re home free.
That doesn’t solve all your problems, since USB doesn’t always allow you to definitively determine which USB device will get which device ID. You can set up udev rules, but in the case of some devices like Arduinos, there is no unique serial number to map to, and in fact, sometimes unplugging and plugging can turn a USB0 into a USB1. But that’s a flaw in hardware and the OS can only do so much. Still, we can make it a little easier by doing this trick.