Reputation: 41
I'm currently working on a project to run an program written in C when a USB device is plugged in. Is this possible with udev rules?
I've currently got it to run a Hello World script when I plug in my device. However, it runs it more than once.
Current path:/etc/udev/rules.d/98-local.rules
Current rule:
SUBSYSTEMS=="usb", ACTION=="add", RUN+="/usr/local/bin/USB.sh"
Script's path: /usr/local/bin/USB.sh
Script:
#!/bin/bash
echo 'Hello World!' >>"/home/<username>/Desktop/udev.out"
exit
I've tried something like this to get the executable to run:
#!/bin/bash
usr/games/blackjack
exit
typing usr/games/blackjack works in the terminal however it doesn't work when the USB device is inserted. However, I know the script is running because I've had them combined in the same file, and the hello world has been created.
I have also tried running the executable from my user account, as follows:
SUBSYSTEMS=="usb", ACTION=='add", RUN+="/bin/su tyler -c '/usr/local/bin/USB.sh'"
However, this doesn't work either.
Is it a problem with device privileges or is it just not possible to run an executable?
*note: I've read the udev rule explanations at http://reactivated.net/writing_udev_rules.html extensively.
Upvotes: 4
Views: 5115
Reputation: 996
Another reason might be that udev seems to run in it's own environment. It's like you would run your program from a tty: It does start, but there's no xserver...
However, this is pure speculation based on my own experiments. It would be nice if someone who knows udev better could confirm this.
EDIT: add the following to your script:
set -x
xhost local:YOURUSERNAME
export DISPLAY=:0.0
And maybe reload your rules with udevadm control --reload-rules
.
(Source: http://ubuntuforums.org/showthread.php?t=994233)
Upvotes: 1
Reputation: 3723
If you write program in C, you can use libudev. It has simple API which allows to enumerate and monitor devices. Here you'll find nice tutorial.
Upvotes: 2
Reputation: 57066
If you can get a shell script to run, you can get an executable to run. It doesn't matter if it was created by C or not.
You seem to be missing a slash. That path almost certainly should be /usr/games/blackjack
, not user/games/blackjack
.
I don't know if you've typed it properly in your terminal and improperly in your script, or if you simply have your environment different. Unless the UDEV system is deliberately designed to recreate your terminal environment, there's no reason why they'd be the same.
Upvotes: 0