Using udev to show a message on drive removal fails

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read how to get help
Post Reply
Sam234
Level 1
Level 1
Posts: 20
Joined: Sun Feb 10, 2013 12:58 pm

Using udev to show a message on drive removal fails

Post by Sam234 » Sun Jul 27, 2014 4:34 pm

I need to give the user a warning once a drive is disconnected, I`ve added the udev rule (in etc/udev/rules.d):

Code: Select all

ACTION=="remove",DRIVERS=="usb",ATTRS{idVendor}=="14cd",ATTRS{idProduct}=="6600", RUN+="/home/me/.bin/unmount70.sh"
and created the script unmount70.sh as

Code: Select all

#!/bin/sh
kdialog --title "Warning!" --passivepopup "Data drive Removed" 5
exit 
but the script does not run on drive removal.

Could this be a privileges issue? or should I do something different.

Thanks

User avatar
WharfRat
Level 21
Level 21
Posts: 13148
Joined: Thu Apr 07, 2011 8:15 pm

Re: Using udev to show a message on drive removal fails

Post by WharfRat » Sun Jul 27, 2014 5:10 pm

Sam234,

That looks like a kde specific application so I'm not at all familiar with it, but to make sure the script is getting hit add

Code: Select all

env >/home/$USER/tstenv.txt
to the script then check that the environment was dumped to that file. If it does, then you can't easily use kdialog for that purpose;Instead try

Code: Select all

notify-send -u normal -t 3600 "Your message" 
Good luck :wink:
ImageImage

Sam234
Level 1
Level 1
Posts: 20
Joined: Sun Feb 10, 2013 12:58 pm

Re: Using udev to show a message on drive removal fails

Post by Sam234 » Sun Jul 27, 2014 5:32 pm

WharfRat wrote:Sam234,

That looks like a kde specific application so I'm not at all familiar with it, but to make sure the script is getting hit add

Code: Select all

env >/home/$USER/tstenv.txt
to the script then check that the environment was dumped to that file.
I`ve added that and there is no dump, so it appears udev is refusing to run my script!

I`ve checked with sudo udevadm test .... for the drive and run: '/home/me/.bin/unmount70.sh' does appear at the end of the output.

User avatar
WharfRat
Level 21
Level 21
Posts: 13148
Joined: Thu Apr 07, 2011 8:15 pm

Re: Using udev to show a message on drive removal fails

Post by WharfRat » Sun Jul 27, 2014 5:40 pm

Try monitoring the activity when you unplug the drive with:

Code: Select all

udevadm monitor --udev
ImageImage

Sam234
Level 1
Level 1
Posts: 20
Joined: Sun Feb 10, 2013 12:58 pm

Re: Using udev to show a message on drive removal fails

Post by Sam234 » Sun Jul 27, 2014 6:02 pm

WharfRat wrote:Try monitoring the activity when you unplug the drive with:

Code: Select all

udevadm monitor --udev

The output is:

Code: Select all

UDEV  [217501.318275] remove   /devices/pci0000:00/0000:00:12.2/usb1/1-1/1-1.2/1-1.2:1.0/host35/target35:0:0/35:0:0:0/scsi_disk/35:0:0:0 (scsi_disk)
UDEV  [217501.318838] remove   /devices/pci0000:00/0000:00:12.2/usb1/1-1/1-1.2/1-1.2:1.0/host35/target35:0:0/35:0:0:0/block/sdb/sdb2 (block)
UDEV  [217501.319840] remove   /devices/virtual/bdi/8:16 (bdi)
UDEV  [217501.320586] remove   /devices/pci0000:00/0000:00:12.2/usb1/1-1/1-1.2/1-1.2:1.0/host35/scsi_host/host35 (scsi_host)
UDEV  [217501.321684] remove   /devices/pci0000:00/0000:00:12.2/usb1/1-1/1-1.2/1-1.2:1.0/host35/target35:0:0/35:0:0:0/block/sdb/sdb1 (block)
UDEV  [217501.342855] remove   /devices/virtual/bdi/8:17-fuseblk (bdi)
UDEV  [217501.348342] remove   /host35/target35:0:0 (scsi)
UDEV  [217501.578674] remove   /devices/pci0000:00/0000:00:12.2/usb1/1-1/1-1.2/1-1.2:1.0/host35/target35:0:0/35:0:0:0/scsi_device/35:0:0:0 (scsi_device)
UDEV  [217501.605283] remove   /devices/pci0000:00/0000:00:12.2/usb1/1-1/1-1.2/1-1.2:1.0/host35/target35:0:0/35:0:0:0/bsg/35:0:0:0 (bsg)
UDEV  [217501.628917] remove   /devices/pci0000:00/0000:00:12.2/usb1/1-1/1-1.2/1-1.2:1.0/host35/target35:0:0/35:0:0:0/block/sdb/sdb5 (block)
UDEV  [217501.628966] remove   /devices/pci0000:00/0000:00:12.2/usb1/1-1/1-1.2/1-1.2:1.0/host35/target35:0:0/35:0:0:0/block/sdb (block)
UDEV  [217501.633256] remove   /devices/pci0000:00/0000:00:12.2/usb1/1-1/1-1.2/1-1.2:1.0/host35/target35:0:0/35:0:0:0/scsi_generic/sg2 (scsi_generic)
UDEV  [217501.635067] remove   /devices/pci0000:00/0000:00:12.2/usb1/1-1/1-1.2/1-1.2:1.0/host35/target35:0:0/35:0:0:0 (scsi)
UDEV  [217501.636484] remove   /devices/pci0000:00/0000:00:12.2/usb1/1-1/1-1.2/1-1.2:1.0/host35 (scsi)
UDEV  [217501.638040] remove   /devices/pci0000:00/0000:00:12.2/usb1/1-1/1-1.2/1-1.2:1.0 (usb)
UDEV  [217501.640630] remove   /devices/pci0000:00/0000:00:12.2/usb1/1-1/1-1.2 (usb)
As there is no mention of the script is it being ignored?

User avatar
WharfRat
Level 21
Level 21
Posts: 13148
Joined: Thu Apr 07, 2011 8:15 pm

Re: Using udev to show a message on drive removal fails

Post by WharfRat » Sun Jul 27, 2014 6:38 pm

I don't see anything amiss with this so I have to ask the dumb questions, does the script run from the cl :?:

Comment the kdialog line and just leave the environment redirect line to see if that alone works.
ImageImage

Sam234
Level 1
Level 1
Posts: 20
Joined: Sun Feb 10, 2013 12:58 pm

Re: Using udev to show a message on drive removal fails

Post by Sam234 » Mon Jul 28, 2014 6:25 am

WharfRat wrote:I don't see anything amiss with this so I have to ask the dumb questions, does the script run from the cl :?:

Comment the kdialog line and just leave the environment redirect line to see if that alone works.

Yes it works fine from the cl

I`ve commented out that line but still no joy!

User avatar
WharfRat
Level 21
Level 21
Posts: 13148
Joined: Thu Apr 07, 2011 8:15 pm

Re: Using udev to show a message on drive removal fails

Post by WharfRat » Mon Jul 28, 2014 10:18 am

I don't know what to tell you now, but this is what I did

/etc/udev/rules.d/98-dongle.rules

Code: Select all

ACTION=="add",DRIVERS=="usb",ATTRS{idVendor}=="154b",ATTRS{idProduct}=="005a",RUN+="/home/bill/script/88-dongle-add.sh"
ACTION=="remove",DRIVERS=="usb",ATTRS{idVendor}=="154b",ATTRS{idProduct}=="005a",RUN+="/home/bill/script/88-dongle-remove.sh"
The two scripts

Code: Select all

#!/bin/bash
# 88-dongle-add.sh
echo "Dongle added" >> /home/bill/script/udevadd.txt
date >> /home/bill/script/udevadd.txt
exit 0

Code: Select all

#!/bin/bash
# 88-dongle-remove.sh
echo "Dongle removed" >> /home/bill/script/udevrem.txt
date >> /home/bill/script/udevrem.txt
exit 0
After plugging the usb dongle in and out

Code: Select all

bill@qiana ~/script $ ls -l udev*
-rw-r--r-- 1 root root 504 2014-07-28 10:10:51 udevadd.txt
-rw-r--r-- 1 root root  44 2014-07-28 10:11:16 udevrem.txt
If you make a change to the rule file make sure to reload them before testing it otherwise it will not work as expected.

Code: Select all

sudo udevadm control --reload-rules
ImageImage

Post Reply

Return to “Newbie Questions”