Search code examples
usbmountsystemd

systemd execute script on (every) mount


I'm tangling with systemd for the first time and cannot really get it to run as I need it, already plowed through Google and the man pages, but no luck..

I need to execute a script every time a USB flash drive is inserted (next step would be to also run a script when the drive is removed.. but first things first..)

I can only get it trigger once, or to trigger in a loop (which luckily quickly fails with a start-limit error) that runs as long as the drive remains connected. I cant get it to only run when it is inserted, and run on every next new insertion.

[Unit]
Description=USB connected trigger
RequiresMountsFor=/media/usb

[Service]
Type=oneshot
ExecStart=/home/hu/hu_restart.sh

I've been playing around with different types and restart options, but no luck.. hu_restart.sh is just a script that runs and exist, it doesn't remain active.

Anyone?

Some background: I've a script running some kind of car "head unit" that needs to know that an USB drive is available (or not) to change the music "source". The hu_restart.sh is a bit of a hack that restarts the head unit script. On start it checks if USB is available and starts playback from there or not.


Solution

  • Couldn't get this to work. But this works quite well using udisks-glue. I believe I read somewhere udisks-glue is deprecated. That may be the case, but it works very well!

    I think it goes beyond the scope to describe this different option here (but, if someone wants to know, I'd be happy to explain).