Automatically running a script when a memory stick is inserted
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Automatically running a script when a memory stick is inserted
Is it possible to automatically run a script or execute a command when a specific memory stick is inserted? What I actually want to do is mount a filesystem contained in a file on the flash drive, I have the relevant entry in the fstab, but that only works if the drive is present during boot.
Yes. You can try to add an udev rule, specific to that particular USB stick. This issue was already discussed some time ago. Take a look here (see posts #3 and #5). Also, here is the official "Writing udev rules" document. See section "Running external programs on certain events".
Strange. If I click on the link of my own post, it comes up. You can try the search form: look for the string "running script USB" and select "Search in titles only" (you have to do it in the Advanced Search). The first hit is the thread I linked. Anyway, the document about Writing Udev Rules, contains all the answers to your question.
The symlink is created in /dev, but the it-works file isn't created by the command. The command I actually want to run, to mount a filesystem image, doesn't work either. I've grepped dmesg and /var/log/messages for udev and for the name of my home directory, but didn't find anything relevant. Can anyone suggest anything?
Since udev does not know about the shell environment you have to use the absolute path to the touch command. Moreover the best way to execute a command is to place it in a script (I guess the touch was only for testing purposes) and put the correct sha-bang #!/bin/bash in it. In this way the shell environment is acquired. Be sure to set permissions 755 to the script.
Anyway I suggest another approach to test your rule. The basic idea is that you have to refine your rule in order to be matched one and only one time during the chain of events performed by udev. I mean if the rule is not enough fine, there is a chance the script is executed more than one time and not only upon inserting the device, but also upon removing it. This is my testing rule:
using the date command and appending (not just redirecting) the output to a file, gives me the ability to test if the script is executed and if it is executed more than one time.
The trick is to chose the rules taking them from the output of
Code:
# udevinfo -a -p /sys/block/sda
Udevinfo starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.
looking at device '/block/sda':
KERNEL=="sda"
SUBSYSTEM=="block"
SYSFS{stat}==" 372 2029 2464 3812 0 0 0 0 0 2258 3812"
SYSFS{size}=="4014080"
SYSFS{removable}=="1"
SYSFS{range}=="16"
SYSFS{dev}=="8:0"
looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host38/target38:0:0/38:0:0:0':
ID=="38:0:0:0"
BUS=="scsi"
DRIVER=="sd"
SYSFS{ioerr_cnt}=="0x0"
SYSFS{iodone_cnt}=="0x2e0"
SYSFS{iorequest_cnt}=="0x2e0"
SYSFS{iocounterbits}=="32"
SYSFS{timeout}=="60"
SYSFS{state}=="running"
SYSFS{rev}=="0.00"
SYSFS{model}=="TS2GJF110 "
SYSFS{vendor}=="JetFlash"
SYSFS{scsi_level}=="3"
SYSFS{type}=="0"
SYSFS{queue_type}=="none"
SYSFS{queue_depth}=="1"
SYSFS{device_blocked}=="0"
SYSFS{max_sectors}=="240"
...omitted...
The /sys/block/sda device id specific for my system and my usb device, so you have first to determine which entry is created after inserting the pen-drive under /sys. Then to match the rule once and only once you have to chose at least one of those fields marked in red. They are specific of the newly added device, whereas the other fields are related to the parent device.
Finally, you can test the udev rule (having the pen-drive inserted) using
Code:
# udevtest /block/sda
again this is specific to my system, but take in mind you have to pass an argument with the /sys part stripped out (cfr. my udevinfo command above). If the rules is correct you should see a line similar to
Code:
main: run: '/usr/local/bin/test.sh'
Also look at /var/log/messages for debug output. For example, you should see something like "udevd-event[17454]: run_program: exec of program '/lib/udev/touch' failed" right now. In summary, if your rule already works, except for the RUN part, you have only to be sure it runs one time only.
Including '"ACTION=="add",' at the beginning of the above rule made the below error appear in /var/log/messages, so I took it out:
Code:
Jan 20 19:57:09 linux-1 udevd[604]: add_to_rules: invalid ACTION operation
Jan 20 19:57:09 linux-1 udevd[604]: add_to_rules: invalid rule '/etc/udev/rules.d/95-msr-usb.rules:1'
For now it shouldn't matter if the command is run twice, although it's something to look at if and when I can get it to run at all.
The test script I'm attempting to run:
Code:
$ cat /home/openSauce/test.sh
#!/bin/bash
date >> test.log
[21:04:14] openSauce@linux-1:~$ ls -l test.sh
-rwxrwxr-x 1 openSauce openSauce 30 2009-01-20 19:42 test.sh
And the result after inserting the pen drive:
Code:
$ ls /dev | grep msr
msr
[21:06:34] openSauce@linux-1:~$ ls /home/openSauce/test.log
ls: cannot access /home/openSauce/test.log: No such file or directory
[21:06:37] openSauce@linux-1:~$
With nothing helpful in messages (these messages all have an earlier timestamp than the most recent insertion of the pendrive, they relate to the situation before I removed 'ACTION=="add"' from the rule):
Code:
# grep udev /var/log/messages
Jan 20 12:01:57 linux-1 kernel: udev: renamed network interface ath0 to wlan0
Jan 20 19:36:55 linux-1 kernel: udev: renamed network interface ath0 to wlan0
Jan 20 19:53:28 linux-1 udevd[604]: add_to_rules: invalid ACTION operation
Jan 20 19:53:28 linux-1 udevd[604]: add_to_rules: invalid rule '/etc/udev/rules.d/95-msr-usb.rules:1'
Jan 20 19:53:28 linux-1 udevd[604]: add_to_rules: invalid ACTION operation
Jan 20 19:53:28 linux-1 udevd[604]: add_to_rules: invalid rule '/etc/udev/rules.d/95-msr-usb.rules:1'
Jan 20 19:57:09 linux-1 udevd[604]: add_to_rules: invalid ACTION operation
Jan 20 19:57:09 linux-1 udevd[604]: add_to_rules: invalid rule '/etc/udev/rules.d/95-msr-usb.rules:1'
Please, can you try the following rule? This time also rename the rule giving a smaller number, since the order of the rules is relevant. In my test I used /etc/udev/rules.d/10-pen-drive.rules.
If ACTION is still not accepted, try to remove it again and re-run. Also, after insertion of the pen-drive, can you post the output of udevtest /block/sdb? Thanks. A last consideration: looking at the output of udevinfo, you don't have any SYSFS{serial} so I'm surprised the rule is matched and creates /dev/msr. Maybe I'm missing something.
Edit: just noticed on some recent systems, like opensuse 11.x, the udevtest command has been replaced by the udevadm tool:
Code:
udevadm test /block/sdb
Last edited by colucix; 01-20-2009 at 02:54 PM.
Reason: a little add-on as per above
# rule and script are as they should be...
[07:45:15] root@linux-1:~# ls -l /home/openSauce/test.sh
-rwxrwxr-x 1 openSauce openSauce 30 2009-01-20 19:42 /home/openSauce/test.sh
[07:45:27] root@linux-1:~# cat /home/openSauce/test.sh
#!/bin/bash
date >> test.log
[07:45:29] root@linux-1:~# ls test.log
ls: cannot access test.log: No such file or directory
[07:45:35] root@linux-1:~# cat /etc/udev/rules.d/10-msr-usb.rules
ACTION=="add", SUBSYSTEM=="block", ATTR{size}=="4108288", ATTRS{serial}=="21135700171F8E04", RUN+="/home/openSauce/test.sh"
# then after the pendrive is inserted and automounted, the script has not run successfully
[07:45:47] root@linux-1:~# ls test.log
ls: cannot access test.log: No such file or directory
The only output of grep udev /var/log/messages with today's date is about my wireless NIC. That's also the only message in dmesg | grep udev. I also grepped /var/log/* but still couldn't find anything.
My system is Fedora 8, apparently it has both udevtest and udevadm, here is the output of both. Hopefully this will tell you something! They do both mention running the script, although there is no evidence of it running or of an error that I can find.
Code:
[07:46:31] root@linux-1:~# udevtest /block/sdb
This program is for debugging only, it does not run any program,
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.
parse_file: reading '/etc/udev/rules.d/05-udev-early.rules' as rules file
parse_file: reading '/etc/udev/rules.d/10-msr-usb.rules' as rules file
parse_file: reading '/etc/udev/rules.d/40-alsa.rules' as rules file
parse_file: reading '/etc/udev/rules.d/40-multipath.rules' as rules file
parse_file: reading '/etc/udev/rules.d/40-redhat.rules' as rules file
parse_file: reading '/etc/udev/rules.d/50-udev-default.rules' as rules file
parse_file: reading '/etc/udev/rules.d/51-vdr.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-cdrom_id.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-libsane.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-net.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-pcmcia.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-persistent-input.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-persistent-storage-tape.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-persistent-storage.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-wacom.rules' as rules file
parse_file: reading '/etc/udev/rules.d/61-persistent-storage-edd.rules' as rules file
parse_file: reading '/etc/udev/rules.d/64-device-mapper.rules' as rules file
parse_file: reading '/etc/udev/rules.d/64-md-raid.rules' as rules file
parse_file: reading '/etc/udev/rules.d/70-persistent-cd.rules' as rules file
parse_file: reading '/etc/udev/rules.d/70-persistent-net.rules' as rules file
parse_file: reading '/etc/udev/rules.d/75-cd-aliases-generator.rules' as rules file
parse_file: reading '/etc/udev/rules.d/75-persistent-net-generator.rules' as rules file
parse_file: reading '/etc/udev/rules.d/80-drivers.rules' as rules file
parse_file: reading '/etc/udev/rules.d/85-pcscd_ccid.rules' as rules file
parse_file: reading '/etc/udev/rules.d/85-pcscd_egate.rules' as rules file
parse_file: reading '/etc/udev/rules.d/90-alsa.rules' as rules file
parse_file: reading '/etc/udev/rules.d/90-hal.rules' as rules file
parse_file: reading '/etc/udev/rules.d/95-msr-usb.rules' as rules file
parse_file: reading '/etc/udev/rules.d/95-pam-console.rules' as rules file
parse_file: reading '/etc/udev/rules.d/95-udev-late.rules' as rules file
parse_file: reading '/etc/udev/rules.d/97-bluetooth.rules' as rules file
parse_file: reading '/etc/udev/rules.d/99-fuse.rules' as rules file
parse_file: reading '/etc/udev/rules.d/xen-backend.rules' as rules file
udevtest: looking at device '/block/sdb' from subsystem 'block'
run_program: '/bin/bash -c '/sbin/lsmod | /bin/grep ^dm_multipath''
run_program: '/bin/bash' (stdout) 'dm_multipath 24401 0 '
run_program: '/bin/bash' returned with status 0
match_rule: set ENV 'DEVTYPE=disk'
run_program: 'usb_id --export /block/sdb'
run_program: '/lib/udev/usb_id' (stdout) 'ID_VENDOR=USB_2.0'
run_program: '/lib/udev/usb_id' (stdout) 'ID_MODEL=Flash_Disk'
run_program: '/lib/udev/usb_id' (stdout) 'ID_REVISION=5.00'
run_program: '/lib/udev/usb_id' (stdout) 'ID_SERIAL=USB_2.0_Flash_Disk_21135700171F8E04-0:0'
run_program: '/lib/udev/usb_id' (stdout) 'ID_SERIAL_SHORT=21135700171F8E04'
run_program: '/lib/udev/usb_id' (stdout) 'ID_TYPE=disk'
run_program: '/lib/udev/usb_id' (stdout) 'ID_INSTANCE=0:0'
run_program: '/lib/udev/usb_id' (stdout) 'ID_BUS=usb'
run_program: '/lib/udev/usb_id' returned with status 0
udev_rules_get_name: add symlink 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
run_program: 'path_id /block/sdb'
run_program: '/lib/udev/path_id' (stdout) 'ID_PATH=pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
run_program: '/lib/udev/path_id' returned with status 0
udev_rules_get_name: add symlink 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
run_program: 'edd_id --export /dev/.tmp-8-16'
run_program: '/lib/udev/edd_id' (stderr) 'no kernel EDD support'
run_program: '/lib/udev/edd_id' returned with status 2
udev_rules_get_name: add symlink 'msr'
udev_rules_get_name: no node name set, will use kernel name 'sdb'
udev_device_event: device '/block/sdb' already in database, cleanup
udev_node_add: creating device node '/dev/sdb', major=8, minor=16, mode=0640, uid=0, gid=6
udev_node_update_symlinks: update symlink 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0' of '/block/sdb'
udev_db_get_devices_by_name: found index directory '/dev/.udev/names/disk\x2fby-id\x2fusb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
update_link: found 1 devices with name 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
update_link: found '/block/sdb' for 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
update_link: compare (our own) priority of '/block/sdb' 0 >= 0
update_link: 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0' with target 'sdb' has the highest priority 0, create it
udev_node_update_symlinks: update symlink 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0' of '/block/sdb'
udev_db_get_devices_by_name: found index directory '/dev/.udev/names/disk\x2fby-path\x2fpci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
update_link: found 1 devices with name 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
update_link: found '/block/sdb' for 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
update_link: compare (our own) priority of '/block/sdb' 0 >= 0
update_link: 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0' with target 'sdb' has the highest priority 0, create it
udev_node_update_symlinks: update symlink 'msr' of '/block/sdb'
udev_db_get_devices_by_name: found index directory '/dev/.udev/names/msr'
update_link: found 1 devices with name 'msr'
update_link: found '/block/sdb' for 'msr'
update_link: compare (our own) priority of '/block/sdb' 0 >= 0
update_link: 'msr' with target 'sdb' has the highest priority 0, create it
udevtest: run: '/home/openSauce/test.sh'
udevtest: run: '/sbin/multipath -v0 8:16'
udevtest: run: 'socket:/org/freedesktop/hal/udev_event'
udevtest: run: '/sbin/pam_console_apply /dev/sdb /dev/disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0 /dev/disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0 /dev/msr'
udevtest: run: 'socket:/org/kernel/udev/monitor'
Code:
[07:47:15] root@linux-1:~# /sbin/udevadm test /block/sdb
This program is for debugging only, it does not run any program,
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.
parse_file: reading '/etc/udev/rules.d/05-udev-early.rules' as rules file
parse_file: reading '/etc/udev/rules.d/10-msr-usb.rules' as rules file
parse_file: reading '/etc/udev/rules.d/40-alsa.rules' as rules file
parse_file: reading '/etc/udev/rules.d/40-multipath.rules' as rules file
parse_file: reading '/etc/udev/rules.d/40-redhat.rules' as rules file
parse_file: reading '/etc/udev/rules.d/50-udev-default.rules' as rules file
parse_file: reading '/etc/udev/rules.d/51-vdr.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-cdrom_id.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-libsane.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-net.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-pcmcia.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-persistent-input.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-persistent-storage-tape.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-persistent-storage.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-wacom.rules' as rules file
parse_file: reading '/etc/udev/rules.d/61-persistent-storage-edd.rules' as rules file
parse_file: reading '/etc/udev/rules.d/64-device-mapper.rules' as rules file
parse_file: reading '/etc/udev/rules.d/64-md-raid.rules' as rules file
parse_file: reading '/etc/udev/rules.d/70-persistent-cd.rules' as rules file
parse_file: reading '/etc/udev/rules.d/70-persistent-net.rules' as rules file
parse_file: reading '/etc/udev/rules.d/75-cd-aliases-generator.rules' as rules file
parse_file: reading '/etc/udev/rules.d/75-persistent-net-generator.rules' as rules file
parse_file: reading '/etc/udev/rules.d/80-drivers.rules' as rules file
parse_file: reading '/etc/udev/rules.d/85-pcscd_ccid.rules' as rules file
parse_file: reading '/etc/udev/rules.d/85-pcscd_egate.rules' as rules file
parse_file: reading '/etc/udev/rules.d/90-alsa.rules' as rules file
parse_file: reading '/etc/udev/rules.d/90-hal.rules' as rules file
parse_file: reading '/etc/udev/rules.d/95-msr-usb.rules' as rules file
parse_file: reading '/etc/udev/rules.d/95-pam-console.rules' as rules file
parse_file: reading '/etc/udev/rules.d/95-udev-late.rules' as rules file
parse_file: reading '/etc/udev/rules.d/97-bluetooth.rules' as rules file
parse_file: reading '/etc/udev/rules.d/99-fuse.rules' as rules file
parse_file: reading '/etc/udev/rules.d/xen-backend.rules' as rules file
udevtest: looking at device '/block/sdb' from subsystem 'block'
run_program: '/bin/bash -c '/sbin/lsmod | /bin/grep ^dm_multipath''
run_program: '/bin/bash' (stdout) 'dm_multipath 24401 0 '
run_program: '/bin/bash' returned with status 0
match_rule: set ENV 'DEVTYPE=disk'
run_program: 'usb_id --export /block/sdb'
run_program: '/lib/udev/usb_id' (stdout) 'ID_VENDOR=USB_2.0'
run_program: '/lib/udev/usb_id' (stdout) 'ID_MODEL=Flash_Disk'
run_program: '/lib/udev/usb_id' (stdout) 'ID_REVISION=5.00'
run_program: '/lib/udev/usb_id' (stdout) 'ID_SERIAL=USB_2.0_Flash_Disk_21135700171F8E04-0:0'
run_program: '/lib/udev/usb_id' (stdout) 'ID_SERIAL_SHORT=21135700171F8E04'
run_program: '/lib/udev/usb_id' (stdout) 'ID_TYPE=disk'
run_program: '/lib/udev/usb_id' (stdout) 'ID_INSTANCE=0:0'
run_program: '/lib/udev/usb_id' (stdout) 'ID_BUS=usb'
run_program: '/lib/udev/usb_id' returned with status 0
udev_rules_get_name: add symlink 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
run_program: 'path_id /block/sdb'
run_program: '/lib/udev/path_id' (stdout) 'ID_PATH=pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
run_program: '/lib/udev/path_id' returned with status 0
udev_rules_get_name: add symlink 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
run_program: 'edd_id --export /dev/.tmp-8-16'
run_program: '/lib/udev/edd_id' (stderr) 'no kernel EDD support'
run_program: '/lib/udev/edd_id' returned with status 2
udev_rules_get_name: add symlink 'msr'
udev_rules_get_name: no node name set, will use kernel name 'sdb'
udev_device_event: device '/block/sdb' already in database, cleanup
udev_node_add: creating device node '/dev/sdb', major=8, minor=16, mode=0640, uid=0, gid=6
udev_node_update_symlinks: update symlink 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0' of '/block/sdb'
udev_db_get_devices_by_name: found index directory '/dev/.udev/names/disk\x2fby-id\x2fusb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
update_link: found 1 devices with name 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
update_link: found '/block/sdb' for 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
update_link: compare (our own) priority of '/block/sdb' 0 >= 0
update_link: 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0' with target 'sdb' has the highest priority 0, create it
udev_node_update_symlinks: update symlink 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0' of '/block/sdb'
udev_db_get_devices_by_name: found index directory '/dev/.udev/names/disk\x2fby-path\x2fpci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
update_link: found 1 devices with name 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
update_link: found '/block/sdb' for 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
update_link: compare (our own) priority of '/block/sdb' 0 >= 0
update_link: 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0' with target 'sdb' has the highest priority 0, create it
udev_node_update_symlinks: update symlink 'msr' of '/block/sdb'
udev_db_get_devices_by_name: found index directory '/dev/.udev/names/msr'
update_link: found 1 devices with name 'msr'
update_link: found '/block/sdb' for 'msr'
update_link: compare (our own) priority of '/block/sdb' 0 >= 0
update_link: 'msr' with target 'sdb' has the highest priority 0, create it
udevtest: run: '/home/openSauce/test.sh'
udevtest: run: '/sbin/multipath -v0 8:16'
udevtest: run: 'socket:/org/freedesktop/hal/udev_event'
udevtest: run: '/sbin/pam_console_apply /dev/sdb /dev/disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0 /dev/disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0 /dev/msr'
udevtest: run: 'socket:/org/kernel/udev/monitor'
[07:47:33] root@linux-1:~#
Hmmm... didn't notice from your previous post, but in your script test.sh you have to use the full path of test.log in order to create it in your home directory. Again the issue is that udev does not know about the shell environment so it has no $HOME and maybe does not have a current working directory at all. So just try to modify the script as
Code:
#!/bin/bash
date >> /home/openSauce/test.log
and please still stick with my last suggested rule (it should take care of running upon insertion only and just one time):
Hmmm... didn't notice from your previous post, but in your script test.sh you have to use the full path of test.log in order to create it in your home directory. Again the issue is that udev does not know about the shell environment so it has no $HOME and maybe does not have a current working directory at all. So just try to modify the script as
Oops, yes, should have noticed that. Got it working now, thanks very much!
One final note for anyone trying to do the same thing (i.e. mount a filesystem image contained on the pendrive):
The pendrive does not mount until after the script test.sh returns. Therefore if you want to do anything with the files on the drive, your script should look something like this:
Code:
{ /bin/sleep 3; your-commands-here; } &
i.e. use sleep to wait for the drive to mount, and use & to get the script to return immediately.
Well done, openSauce! Regarding your last statement, I would add that you should try to locate which is the rule that mounts the USB device and place the newly created rule after that. Looking at your output in one of the posts above, the matching rule for mounting USB device can be /etc/udev/rules.d/50-udev-default.rules. If you try to rename your rule as 55-msr-usb.rules, maybe the device will be already mounted when you try to access files on it.
Here is the relevant section of the Writing Udev Rules manual:
Quote:
Files in /etc/udev/rules.d/ are parsed in lexical order, and in some circumstances, the order in which rules are parsed is important. <omitted>. One device can be matched by more than one rule. This has it's practical advantages, for example, we can write two rules which match the same device, where each one provides its own alternate name for the device. Both alternate names will be created, even if the rules are in separate files. It is important to understand that udev will not stop processing when it finds a matching rule, it will continue searching and attempt to apply every rule that it knows about.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.