How to run a Python script when inserting a USB flash drive

My goal is to run a Python script when inserting a USB flash drive. I wrote a udev rule and a shell script that is called in this rule.

udev rule: /etc/udev/rules.d/10-usb.rules

KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", RUN+="/home/Hypotheron/Desktop/script.sh" 

script.sh:

#!/bin/sh

echo 'Hello, world.' > /home/Hypotheron/Desktop/foo.txt
#/home/Hypotheron/Desktop/job.py & exit

The first line of my Python file:

#!/usr/bin/python 

I also executed the following commands:

chmod +x job.py
chmod +x script.sh

In script.sh, when a line entry in foo.txt is uncommented, the foo.txt file is created each time a flash drive is inserted.

When I comment on this line and uncomment the line in which the Python file runs, it does not work.

Running the terminal script.sh through both works, but when you insert a flash drive, only the foo.txt file works.

Any help would be appreciated.

+4
source share
1
   RUN{type}
       Add a program to the list of programs to be executed after
       processing all the rules for a specific event, depending on "type":

       "program"
           Execute an external program specified as the assigned value. If
           no absolute path is given, the program is expected to live in
           /lib/udev; otherwise, the absolute path must be specified.

           This is the default if no type is specified.

       "builtin"
           As program, but use one of the built-in programs rather than an
           external one.

       The program name and following arguments are separated by spaces.
       Single quotes can be used to specify arguments with spaces.

       This can only be used for very short-running foreground tasks.
       Running an event process for a long period of time may block all
       further events for this or a dependent device.

       Starting daemons or other long-running processes is not appropriate
       for udev; the forked processes, detached or not, will be
       unconditionally killed after the event handling has finished.

udev 2 .
,

+2

Source: https://habr.com/ru/post/1680681/


All Articles