I found several questions on http://unix.stackexchange.com & on http://stackoverflow.com related to launch script by udev rules. But I can't find exact solution or certain answer for executing Bash script.
So, I'm asking the (canonical) question: How to run/launch/execute Bash script by means of udev rules?
Another thing that I found on Writing udev rules is:
udev does not run these programs on any active terminal, and it does not execute them under the context of a shell. Be sure to ensure your program is marked executable, if it is a shell script ensure it starts with an appropriate shebang (e.g. #!/bin/sh), and do not expect any standard output to appear on your terminal.
Hence, I want to know if possible that "How to run bash script by udev rules?"
Here is simple example of executing bash-script.
I've written 99-myrule.rules
under /etc/udev/rules.d
which contains:
ACTION=="add", ATTR{idVendor}=="0781", ATTR{idProduct}=="5567", RUN+="/home/pandya/example.sh"
I found Vendor & Product ID from lsusb
output:
$ lsusb | grep SanDisk
Bus 001 Device 066: ID 0781:5567 SanDisk Corp. Cruzer Blade
And I want to execute experimental command zenity --info
when my pendrive (of SanDisk listed above) attached.
so, I've writted /home/pandya/example.sh
(which is called from RUN+=
in udev rules) contians:
#!/bin/bash
zenity --info &
exit
But This script is not executing (in-spite of having execution permission). I've also tried following:
export DISPLAY=:0
in script beforezenity --info
bash -c 'DISPLAY=:0 zenity --info
And of-course:
sudo restart udev
sudo udevadm control --reload-rules
But I can't get output zenity --info
.
Hence How can I run Bash script by menas of udev rules? Suggest me if I'm missing anything.
su - your_X_user_here -c 'export DISPLAY=:0 zenity --info'
? – YoMismo Jan 07 '15 at 07:33su pandya -c "DISPLAY=:0 zenity --info"
works! – Pandya Jan 07 '15 at 10:13su <username> -c "DISPLAY=:0 bash -c /path/to/script.sh"
is the answer – Pandya Jan 07 '15 at 10:18