如何在插入USB闪存驱动器时运行Python脚本

问题描述:

我的目标是在USB闪存驱动器插入时运行Python脚本。我写了一个udev规则和一个在该规则中调用的shell脚本。如何在插入USB闪存驱动器时运行Python脚本

udev规则:/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 

我的Python文件的第一行是:

#!/usr/bin/python 

我还做了以下这些命令:

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

在写入foo.txt的行被取消注释时,script.sh中的每个闪存驱动器插入都会创建foo.txt文件。

当我评论该行并取消注释运行Python文件的行时,它不起作用。

通过终端运行script.sh可以在两种情况下都能正常工作,但插入闪存驱动器时只能使用foo.txt格式。

任何帮助,将不胜感激。

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段。
我的猜测是,你已经发现了无条件致命的部分