Udev rule - run script when USB mounted

Hi All,

I’m working on a custom OSMC script. I have a script I’ve completed already, that runs a slideshow from the root folder of a USB of any USB device inserted.

I want to create a udev rule that automatically runs the slideshow.py script whenever a new USB device is mounted.

I have a udev rule file, /etc/udev/rules.d/99-slideshow.rules

ACTION==“add”, SUBSYSTEMS==“usb”, RUN+="/home/osmc/.kodi/userdata/slideshow.py"

How do I format the above udev rule so it will run whenever a new USB device is added to the device?
Much Thanks!

We use udisks-glue. See advice here (mentioning udisks-glue):

You can even make sure USB disks with your specific label are only processed which is great if this is a commercial endeavour.

It seems from Reddit you may be embarking on a commercial project.
Good luck with this.

Please don’t use our project to promote piracy and make sure you comply with the GPLv2. If you’re making modifications to OSMC and redistributing it, we’d also appreciate you removing OSMC branding as it is no longer an OSMC product.


Okay, I’m a little confused by the udisks-glue documentation. I can mess around with this tomorrow. Thinking out loud-

1.) install udisks-glue.conf, which I need to figure out how to do…
2.) make a home/udisks-glue.conf
3.) Add the following to home/udisks-glue.conf:

filter USBDRIVE {
optical = false
partition_table = false
usage = filesystem
match USBDRIVE {
post_mount_command = “path/to/my/python/script.py”

This seems to be where I’m headed.


We have a udisks-glue.conf in /etc.

Probably just needs appending.

Make sure you chmod +x your .py as well so it has execute permission

I’ve been messing on udisks-glue.conf, no luck yet. Here’s what I’ve tried:

I’ve tried adding as the last line of match disks {

post_mount_command = “/path/to/my/script.py”
} with the correct path

I’ve tried adding a line to the post_insertion_command, replacing fi’ with fi

filter usbdrive {
optical = false
partition = false
usage = filesystem

match usbdrive {
post_mount_command = “path/to/script.py”

I’ve chmod +x my script, and it will work when it’s run as an RunScript(/path/to/script.py).

Is it syntax, or something more fundamental? I’ve tried post_mount_command = path/to/script.py without quotations, for instance.

Thanks for all the help, this is a very low-scale project right now, not sure if it will be redistributed, I’ll review the GPLv2 and make sure to remove any OSMC branding.

I haven’t tried Udev rules, but the quoted line may need to look like this:

post_mount_command = "python /path/to/my/script.py"

or maybe even:

post_mount_command = "/usr/bin/python /path/to/my/script.py"

I can give it a try, but I have a shebang in my python script, #!/usr/bin/python, so I’m not sure if the above changes are needed. I can run my script from debian just entering the filepath, for instance. My script won’t work in shell because it can’t import XBMC, though.

The shebang is handled by bash, so you normally will need to specify python. Same if you were setting up a cron job.

I’m finding myself pretty confused by udisk-glue.conf and udev rules, like on a more basic level of how the daemon operates.

sam_nazarko: udisks-glue looks like a handy daemon, I just can’t seem to figure it out, there’s no tutorials and little code online that references it, other than your example. Here’s what I came up with based on your guidance/example:

filter usbdev {
optical = false
usage = filesystem
match usbdev {
post_mount_command = “/usr/bin/python /path/to/script.py”

I added the above into my udisks-glue.conf file in /etc/ and no luck.

Udev: more examples online. I found this post which suggests that you can’t run a script directly through udev, but you could start a systemd service that can execute my python shell script.

My python shell script searches through /media/, saves a filepath to a USB drive, then starts a XBMC command that launches a slideshow from that filepath. It works great from the menu in a custom menu action RunScript(/path/to/python/script.py).

I seem to have bit off more than I can chew with this automatic slideshow launch on USB mount…

I currently don’t have physical access to my Pi or Vero to test this out, so apologies if I’m off target, but your udisks-glue.conf file looks like the match usbdev {} section is missing the line automount = true and possibly automount_options = { blah, blah }, so the post_mount_command won’t run until something has actually been mounted.

You are not alone.
UDisks is a cumbersome beast and we are moving away from it in the future. I think it’s important to mention that – of course older versions of OSMC would remain working.

If you want a platform independent solution, write an inotify daemon which triggers on /media changes. You can almost certainly do this with Python.

Then regardless of OSMC’s disk mounting implementation, you’ll be unaffected by such changes.

Hope this helps