2017-07-04 99 views
4

我的目標是在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格式。

任何幫助,將不勝感激。

回答

2
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段。
我的猜測是,你已經發現了無條件致命的部分

相關問題