How to run Python script on USB flash-drive insertion
Asked Answered
N

1

8

My goal is to run a Python script on USB flash-drive insertion. I have written a udev rule and a shell script that is called in that 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 is:

#!/usr/bin/python 

I also did these following commands:

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

In the script.sh when the line writing to foo.txt is uncommented the foo.txt file is created every flash-drive insertion.

When I comment that line and uncomment the line running the Python file it doesn't work.

Running the script.sh through terminal works in both cases but when inserting a flash-drive only the foo.txt case works.

Any help would be appreciated.

Nubble answered 4/7, 2017 at 4:25 Comment(0)
Y
3
   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.

From the udev man page, pay special attention to the last 2 paragraphs.
My guess, is that you have discovered the unconditional killing part

Edit 1 Year later:
I revisited this after someone voted it up and I have resolved the issues, which are, that root (who is running this process) does not have an X terminal entry essential for certain things like notify-send or starting a Gui program and there still remains the killing of the process after the event, as mentioned previously.
The following sends a notification to the terminal and starts a wxPython Gui program, when a usb device is inserted.

The script:

#!/bin/sh
DISPLAY=:0
export DISPLAY
/usr/bin/notify-send "Usb Device detected" "Starting Reminder program" | at now
/usr/bin/python3 /home/rolf/reminders/reminders2.1.0/reminder.py | at now

by defining DISPLAY we get around root's issue of no X term entry
by passing the commands that we wish to run, to the at program with the instruction to run it now, we avoid the udev killing of the process.

The /lib/udev/rules.d/10-usbinsert.rules file:

KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", RUN+="/usr/bin/sudo -u rolf /home/rolf/script.sh &"

I hope this helps or gets you moving in the right direction.

Yanyanaton answered 4/7, 2017 at 6:4 Comment(3)
I know this is old but I too would like to start a python program that starts on a USB device plug in event. The program reads data from the device. Being new to Linux I thought this was easily done using udev rules. I had no knowledge of the program being killed. However, you seem to have found a workaround but I don't understand how your script works. I do not know what the 'at' program is. The notify-send docs were not very much help.Negotiation
@BrianReinhold at - at, batch, atq, atrm - queue, examine, or delete jobs for later execution, install it using your package management program. On my system that's sudo apt install at then you can use the manual to see its options man atYanyanaton
Thanks Rolf. Hoping to get around the 'kill application' part.Negotiation

© 2022 - 2024 — McMap. All rights reserved.