I created a small sample-script which is reduced version of my real script and it looks basically as follows:
#!/bin/bash
echo "start" > /home/myName/log.txt
#get list with all attached devices
for i in $(lsblk -lo name,fstype,hotplug,type|grep '1 part$'|tr -s ' ' ' '|sed 's/ 1 part$//'|grep ' ..*$'|tr ' ' '_')
do
echo $i >> /home/myName/log.txt
done
If i call the script directly in the terminal, everything works correctly. So the return in the logfile is something like that.
start
sdc1_vfat
But as soon as the script is called from a udev rule, the loop does not work anymore. The output in the logfile is only
start
The loop does not work anymore.
The rule in the /dev/udev/ looks as follows
SUBSYSTEM=="usb", ACTION=="add", RUN+="/bin/bash /usr/bin/myScript.sh"
Anyone an idea what the cause could be?
Here some more information and as suggested a reworked version.based on your answers.
Here the udev rule:
# check usb sdb1 plugged unplugged
SUBSYSTEM=="block", ACTION=="add", RUN+="/bin/bash /usr/bin/usb_mount.sh"
SUBSYSTEM=="block", ACTION=="remove", RUN+="/bin/bash /usr/bin/usb_unmount.sh"
The mounting scriupt does now looks as follows:
#!/bin/bash
#general definitions
MNT_PATH="/media/USB_DRIVE"
DEV_ID="sdc1"
check connected device
udevadm info -q all "/dev/$DEV_ID" | tr '\n' ' ' | grep "/dev/$DEV_ID" | grep "ID_FS_VERSION=FAT32"
check return
if [ $? -eq 0 ]
then
create folder and mount
mkdir -p $MNT_PATH
  mount -o rw,user,exec,umask=0000 "/dev/$DEV_ID" $MNT_PATH
  exit
fi
If I check the mounting command, the exit code is 0 so it has been processed successfully.
The folder is getting created correctly, but the drive is not mounted.
If the script is called directly in the terminal, everything works as expected.
But if it's called by the udev it is not mounted.
I still don't now the problem:
- Is it a timing problem - even if the udevadm info -q all /dev/sdb*returns a connected device?
- Is it a permission or path problem?
Any ideas how I can find the cause?
Additional Information
Just figured out, that in the syslog an exit code 127 is reported.
So it seems to be a permission problem. But how can I ensure the udev script runs as root?
 
     
    