Whi*_*oln 5 bash scripts usb udev
我无法让 udev 运行的脚本在 USB 插入时在后台运行。
我的 udev 规则似乎有效,因为它肯定会调用脚本,但是无论我做什么,我都无法让 bash 脚本在后台运行,因此它会阻塞。
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"
#!/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
Run Code Online (Sandbox Code Playgroud)
#!/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
Run Code Online (Sandbox Code Playgroud)
在 10 秒完成之前,usb 不会被 nautilus 挂载,并且 udisksctl 命令Error looking up object for device /dev/usb/adata1在取消注释时给我错误,这让我认为 udev 规则甚至还没有完成符号链接的制作。
请注意,当我从终端而不是 udev 运行该脚本时,它可以正常工作
RUN 只能用于短期任务。
RUN{type}Run Code Online (Sandbox Code Playgroud)... 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
您可以使用disown它将先前的进程与当前的 shell 分离。
#!/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
Run Code Online (Sandbox Code Playgroud)
这与通过 /etc/udev/rules.d/ 连接时在 USB 中写入文件的情况类似,您也可以看看这个https://askubuntu.com/a/635477/26246 by Fëamarto,很好的递归技巧等待分区挂载。
更新
udev这些年来变化很大。它成为了一部分,systemd并且更加局限。以下是当前的选项:
| 归档时间: |
|
| 查看次数: |
5312 次 |
| 最近记录: |