How to autorun a script when you plugin a USB on Toradex Device

Hi Toradex Community,

How to autorun a script when you plugin a USB on Toradex Device?

Best Regards,

Ahmed49

Hi, I’m the Toradex AI Assistant. I’m in early beta, and I would appreciate your feedback.
The Toradex Support team will follow up with you shortly!

Did you like my answer?

:+1: :-1:


Hello,

To autorun a script when you plug in a USB on a Toradex device running Torizon, you can utilize udev rules. udev is the device manager for the Linux kernel that manages device nodes in the /dev directory. By creating a custom udev rule, you can specify actions to be taken when a USB device is inserted.

Here’s a step-by-step guide to achieve this:

  1. Create a Udev Rule: Create a file in /etc/udev/rules.d/ with a descriptive name, for example, 99-usb-autorun.rules. The naming convention is important; rules are processed in lexical order.

    sudo nano /etc/udev/rules.d/99-usb-autorun.rules
    
  2. Define the Rule: In the file, define a rule that matches your USB device and specifies the script to be executed. For example, to run a script for any USB device, you might write:

    ACTION=="add", SUBSYSTEM=="usb", RUN+="/path/to/your/script.sh"
    

    Adjust the SUBSYSTEM and other match keys as needed to target specific devices or types of devices. The ACTION=="add" part ensures the script runs when the device is plugged in.

  3. Create the Script: Ensure the script you want to run (/path/to/your/script.sh in the example) exists and is executable. For instance:

    #!/bin/bash
    echo "USB device plugged in" >> /var/log/usb-plug.log
    

    Make sure to give it execute permissions:

    sudo chmod +x /path/to/your/script.sh
    
  4. Reload Udev Rules: After creating the rule and the script, reload the udev rules to apply the changes:

    sudo udevadm control --reload-rules
    sudo udevadm trigger
    
  5. Testing: Plug in a USB device to test if the script runs as expected. Check the logs or the effects of your script to verify.

This method allows for a high degree of customization, letting you specify different scripts for different devices or actions. Remember to replace /path/to/your/script.sh with the actual path to your script and adjust the udev rule according to your specific requirements.

Best regards,
Toradex AI Assistant

Please, also check if these links can help you solve your problem:

Loading Toradex Easy Installer | Toradex Developer Center.

Greetings @Ahmed49,

Our ToradexAI bot does provide one possible solution. Using udev rules you can have scripts executed when certain hardware is detected by the kernel. This would probably be the most straightforward path if you expect the USB device to be attached at any time while the system is running.

Otherwise, you would need to create some kind of program/script that polls the system for the USB device you’re looking for in the background. Then, execute some script upon detection.

May I ask what kind of use-case are you trying to achieve here?

Best Regards,
Jeremias