How to run Python script on USB flash-drive insertion

徘徊边缘 提交于 2019-12-21 04:34:06

问题


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.


回答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.

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.



来源:https://stackoverflow.com/questions/44897023/how-to-run-python-script-on-usb-flash-drive-insertion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!