bipbip38
Posts: 5
Joined: Thu Aug 15, 2019 5:27 pm

UDEV rules

Thu Nov 07, 2019 8:49 pm

Hello,

I am looking to launch a program when an SD card is inserted (as the pop-up window appears asking me what to do with the opening of this SD card).

I do not know if the code is good ?

Code: Select all

#Start action when the SD carde is connected

KERNEL=="mmcblk0*", run+="/home/pi/Desktop/start_command.py"
I do not know where to position the rule?
/lib/udev/rules.d/10-Start-SD.rules
Thank you for your help.

User avatar
topguy
Posts: 7053
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: UDEV rules

Thu Nov 07, 2019 10:20 pm

udev rules are tricky.

- Your rule might work. I've never tested anything similar.
- /etc/udev/rules.d/ is where you should put the file.
- Your main problem is that "udev" will not allow you to start a script that will run for more than 1-2 seconds, and you can not use the script to execute another script either, both will be killed within a few seconds.
- And even if your start_command.py is a very short thing I have never tried to start a Python script this way. Using "/usr/bin/python /home/pi/Desktop/start_command.py" migth work better.

What are you planning to do in the python script ?

bipbip38
Posts: 5
Joined: Thu Aug 15, 2019 5:27 pm

Re: UDEV rules

Thu Nov 07, 2019 11:22 pm

Ah ben mince alors. J'ai vu nul part une telle limitation.

Non, le programme peux durer plusieurs heures et j'ai absolulent besoin qu'il se lance lorsqu'une carte SD est insérée.

J'ai essayé de trouver le programme qui ouvre automatiquement une fenêtre demandant "si on veut ouvrir l'explorateur sûr la carte SD" dès son insertion, mais sans succès.
Sinon j'aurais essayé d'y rajouter une ligne pour exécuter le code python.

Vous avez une solution pour ma problématique ?

Je vous remercie par avance.

User avatar
topguy
Posts: 7053
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: UDEV rules

Thu Nov 07, 2019 11:31 pm

"oui" ,"non", "pommes de terre"

Thats about all the french words I know....

bipbip38
Posts: 5
Joined: Thu Aug 15, 2019 5:27 pm

Re: UDEV rules

Fri Nov 08, 2019 9:55 am

Sorry, the translation.

Damn. I have seen no such limitation.

No, the program can last several hours and I absolutely need it to start when an SD card is inserted.

I tried to find the program that ran automatically in an advanced window "if you want to open the safe explorer SD card" from its insertion, but without success.
Otherwise I would have tried to add a line to execute the python code.

Do you have a solution for my problem?

I thank you in advance.



topguy

User avatar
topguy
Posts: 7053
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: UDEV rules

Fri Nov 08, 2019 11:55 am

You need to have your script registered as a "systemd" service and then you can trigger that service from the udev rule.

This link: https://coreos.com/os/docs/latest/using ... rules.html has the most concise description of such a solution that I can find with a quick google, but you have to ignore the parts about the virtualisation.

There should be some earlier threads in the forum regarding people that want to autocopy files from usb-memsticks automatically that also should contain similar information.

----

A more "hacky" way would be that your .py script is always running but it will not do anything until it find the file "/tmp/ready.txt", and the only command you need to do in the udev rule is to create the file with "touch /tmp/ready.txt".

Return to “Raspberry Pi OS”