2017-07-04 38 views
4

私の目標は、USBフラッシュドライブ挿入時にPythonスクリプトを実行することです。私はudevルールとそのルールで呼び出されるシェルスクリプトを書いています。USBフラッシュドライブ挿入時にPythonスクリプトを実行する方法

udevのルール:

KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", RUN+="/home/Hypotheron/Desktop/script.sh" 

script.sh /etc/udev/rules.d/10-usb.rules:

#!/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 

script.shで、foo.txtへの書き込みがコメントアウトされている場合、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つの段落に特に注意してください。
私の推測では、あなたは無条件の殺害部分を発見したということです

関連する問題