Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
770 views
in Technique[技术] by (71.8m points)

linux - Bash script to detect when my USB is plugged in and to then sync it with a Directory

Is there a Bash script and/or daemon that I can write that will detect a specific USB drive and then sync that drive with a directory?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For future reference, here's how to run a bash script upon detection of a USB drive.

Connect your device and run lsusb to retrieve the device's info. You should see something similar to this:

$ lsusb
Bus 002 Device 039: ID 0bc2:2100 Seagate RSS LLC

In this case, the vendor ID of the device is 0bc2 and the product ID is 2100.

Now you can create your UDEV rule using a text editor of your choice.

$sudo vi /etc/udev/rules.d/85-my_usb_device_rule.rules

And add this:

ACTION=="add", SUBSYSTEM=="usb", SYSFS{idVendor}=="0bc2", SYSFS{idProduct}=="2100", RUN+="/home/myhome/my_script"

/home/myhome/my_script is the path to your script which it would do whatever you want.

To make sure the detection script will execute right away, run this command to reload the UDEV rules:

$sudo udevadm control --reload-rules

This was tested on Fedora 14.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...