Run script with udev after USB plugged in on RPi

为君一笑 提交于 2019-12-08 06:01:45

问题


I am trying to run a script from a udev rule after any USB drive has been plugged in.

When I run the script manually, after the USB is mounted normally, it will run fine. The script calls a python program to run and the python program uses a file on the USB drive. No issues there. If I make the script to simply log the date in a file, that works just fine. So I know my UDEV rule and my script work fine, each on their own.

The issue seems to come up when udev calls the script, then script calling the python program and the python program does not run right. I believe it to be that the USB drive has not finished mounting before the python script runs. When watching top, my script begins to run, then python begins to run, they both end, and then I get the window popup of my accessing the files on my USB drive.

So I tried having script1.sh call script2.sh call python.py. I tried having script.sh call python1.py call python2.py. I tried adding sleep function both in the script.sh and python.py. I tried in the rule, RUN+="/home/pi/script.sh & exit". I tried exit in the files. I tried disown in the files.

What else can I try?


回答1:


Well you probably described you problem. The mount process is too slow. You can mount your usb device from your script.sh

Also you probably need to disable automatic USB device mount for your system or the specific device only.

If you add a symlink to your udev rule e.g. SYMLINK+="backup", then you can mount this device by:

mkdir -p /path/to/foo mount -t ext4 /dev/backup /path/to/foo



来源:https://stackoverflow.com/questions/34295198/run-script-with-udev-after-usb-plugged-in-on-rpi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!