I'm hoping to setup a script to run automatically when a USB device is connected, however I'm not certain how to trigger it.

Unfortunately the USB device isn't a volume, so I can't use the onmount trigger for it, and while I have a script command to interact with it, it doesn't include an API to trigger actions automatically; i.e- I can verify that it's connected, but I'd have to poll continuously until it is. I don't expect to be able to get the developers to add such functionality any time soon either (if at all).

So, is there a generic way that I can configure a launchd task that will trigger when a USB device is detected, and thus avoid the need to poll for it? If this isn't possible then are there any other alternatives capable of triggering a shell script when I connect the device?

  • 2
    Did you already check this SO question? – klanomath Apr 15 '15 at 23:26
  • Didn't come up in my search, so thanks for the link! Sounds like launchd won't do for shell scripting (since a shell script can't call the necessary API once it launches), but I may have found a workaround that will create a file in a known location, so I can instead watch for that; not pretty, but it'll do! Not sure what to do about this issue though, as the duplicate is on a different site? – Haravikk Apr 16 '15 at 11:04
  • I think the question is fine here. – klanomath Apr 16 '15 at 12:02
  • @Haravikk you can call the necessary API by using this amazing utility that will call xpc_set_event_stream_handler to remove the event from the queue, and then call your desired application. Allowed me to come up with a solution using shell scripting. Check out github.com/snosrap/xpc_set_event_stream_handler – zen Jul 1 at 20:54

You must log in to answer this question.

Browse other questions tagged .