udev脚本不在后台运行

我无法让udev运行的脚本在usb insert的后台运行。

我的udev规则似乎有效,因为它肯定会调用脚本,但无论我做什么,我都无法让bash脚本在后台运行,因此它会阻塞。

以供参考:

我的udev规则:

ATTRS{idVendor}=="125f", ATTRS{idProduct}=="db8a", SYMLINK+="usb/adata%n", ENV{XAUTHORITY}="/home/abe/.Xauthority", ENV{DISPLAY}=":0", OWNER="abe", RUN+="/home/abe/bin/usb-adata_udev.sh"

我的bash脚本:

 #!/bin/bash if [[ $ACTION == "add" ]]; then # I've tried many variations on this, none seem to work (su abe /bin/bash -c "/home/abe/Documents/Programs/USB\ Sync/usb-in.sh") & fi if [[ $ACTION == "remove" ]]; then /home/abe/Documents/Programs/USB\ Sync/usb-out.sh & fi 

后台脚本:

 #!/bin/bash #echo $ACTION > "/home/abe/Desktop/test.txt" if [[ ! -d "/media/abe/ABE" ]]; then # for testing sleep 10 #udisksctl mount -b /dev/usb/adata1 &> "/home/abe/Desktop/test.txt" #rsync --update /media/abe/ABE/Files/db.kdbx /home/abe/Documents/db.kdbx echo "FINISHED" >> "/home/abe/Desktop/test.txt" fi 

在10秒完成之前,usb不会被nautilus挂载,并且udisksctl命令给我错误在取消注释时Error looking up object for device /dev/usb/adata1 ,这让我觉得udev规则没有’甚至完成了符号链接。

请注意,当我从终端而不是udev运行脚本时,脚本运行正常

RUN只能用于短期任务。

RUN{ type }

 ... This can only be used for very short-running foreground tasks. Running an event process for a long period of time may block all further events for this or a dependent device. Starting daemons or other long running processes is not appropriate for udev; the forked processes, detached or not, will be unconditionally killed after the event handling has finished. 

来源: man udev

您可以使用将从当前shell分离前一个进程的disown

 #!/bin/bash if [[ $ACTION == "add" ]]; then # I've tried many variations on this, none seem to work (su abe /bin/bash -c "/home/abe/Documents/Programs/USB\ Sync/usb-in.sh") & disown fi if [[ $ACTION == "remove" ]]; then /home/abe/Documents/Programs/USB\ Sync/usb-out.sh & disown fi 

这是类似于在/etc/udev/rules.d/连接时在usb中写文件的情况,也许你可以通过Fëamarto来看看这个https://askubuntu.com/a/635477/26246 ,很好的递归技巧等到分区安装完毕。