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?
lsblk -lo name,fstype,hotplug,type|grep '1 part$'|tr -s ' ' ' '|sed 's/ 1 part$//'|grep ' ..*$'|tr ' ' '_' >> /home/myName/log.txt
replaces the whole loop. – mook765 Jul 06 '22 at 22:22lsblk
wont have the device untiludev
has finished doing its thing(including processing the rules file you created) ... i.e. your script runs but the output fromlsblk
is empty ...lsblk
wont work, so find another way. – Raffa Jul 07 '22 at 07:28udev
finishes its thing, no information is available tolsblk
no matter what you do ... but it's available toudev
itself ... so useudevadm info -q all /dev/sdb*
instead – Raffa Jul 07 '22 at 08:04udev
rule needed ... another approach for monitoringinotifywait -m --include "/dev/sdb1" -e create -e delete /dev/ | while read -r directory event partition; do echo "$directory$partition was $event"; done
– Raffa Jul 08 '22 at 07:16/bin/bash
when the given command is not found … Specify the full path to executables in your script to solve that … But, even then, mount will not work while your rule is holdingudev
from finishing … try another monitoring aproach. – Raffa Jul 08 '22 at 07:37exit code 127
disappears. But the device still does not get mounted. But how can it be, that the mounting command in the script returns theexit code 0
(success) and the device does not get mounted? In my point of view, it should throw an error blocked by another process, it should throw an error. Otherwise the diagnosis is quite impossible... – hafisch Jul 08 '22 at 09:16udev
rules are a bit weird when it comes to executing scripts that try to mount the device triggering the rule. – Raffa Jul 08 '22 at 12:58udev
and theudisks
utilities) … try it and if it works for you, then I suggest you change the title of this question to reflect “mounting a disk partition by name/dev/sdb1
when it becomes available on the system” and perhaps add a first paragraph explaining what you wand to achieve from your script i.e. make this post easier to find by people who tackle the same problem. – Raffa Jul 08 '22 at 15:49