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

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

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中,每个闪存驱动器插入都将创buildfoo.txt文件。

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

在两种情况下,通过terminal运行script.sh工作,但插入闪存驱动器只有foo.txt的情况下工作。

任何帮助,将不胜感激。

Solutions Collecting From Web of "如何在USB闪存驱动器插入上运行Python脚本"

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