Shell file that finds a flash drive on my Raspberry Pi and executes a given program

985 views Asked by At

I am working on a robot and I need to be able to quickly load in new code. So far I have exhausted my other options (including using the micro sd card. we would prefer to leave that in the pi to prevent it getting lost. we are still putting the auto-run file on the microsd card though). I am running Arch Linux on the Raspberry Pi. What I need is a shell file that auto runs on boot up (or when a flash drive is plugged in). The shell file needs to run a python program called main.py and a .sh file called update.sh. I am not asking for you to just give me the file that does exactly what I want (would be appreciated though) but rather I am looking for the resources I need to do this. If it helps, there will only be one USB flash drive at any given time.

2

There are 2 answers

0
shivams On

This can be done using udev rules. First you will need to note down some of the attributes of your USB using lsusb:

$ lsusb
Bus 003 Device 011: ID 0bc2:2100 Seagate RSS LLC 

Now, here

Vendor ID  ==> 0bc2
Product ID ==> 2100

Note them down for your specific USB drive. Now create a new udev rule in a new file /etc/udev/rules.d/myrule.rules and add this line in it:

ATTRS{idVendor}=="0bc2", ATTRS{idProduct}=="2100", RUN+="/home/main.py"
ATTRS{idVendor}=="0bc2", ATTRS{idProduct}=="2100", RUN+="/home/update.sh"
0
Matt On

I know this answer is late, but maybe it will help someone else in the future.

More general, you can make the line inside the .rules file:

KERNEL==”sd[a-z]”, SUBSYSTEM==”block”, ACTION==”add”, RUN+=”/home/pi/filename.sh”  

I did not have any luck running a python file directly from udev, but I created a .sh file and ran the python file from with that.

filename.sh:

#!/bin/sh
Python /home/pi/PythonScriptName.py