如何在USB闪存驱动器插入上运行Python脚本 [英] How to run Python script on USB flash-drive insertion

查看:78
本文介绍了如何在USB闪存驱动器插入上运行Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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规则:/etc/udev/rules.d/10-usb.rules

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:

script.sh:

#!/bin/sh

echo 'Hello, world.' > /home/Hypotheron/Desktop/foo.txt
#/home/Hypotheron/Desktop/job.py & exit

我的Python文件的第一行是:

The first line of my Python file is:

#!/usr/bin/python 

我还执行了以下命令:

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

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

In the script.sh when the line writing to foo.txt is uncommented the foo.txt file is created every flash-drive insertion.

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

When I comment that line and uncomment the line running the Python file it doesn't work.

在两种情况下都可以通过终端运行script.sh,但是在插入闪存驱动器时,仅foo.txt情况有效.

Running the script.sh through terminal works in both cases but when inserting a flash-drive only the foo.txt case works.

任何帮助将不胜感激.

推荐答案

   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手册页上,请特别注意最后两段.
我的猜测,是你发现了无条件杀戮部分

From the udev man page, pay special attention to the last 2 paragraphs.
My guess, is that you have discovered the unconditional killing part

编辑1年后:
在有人投票赞成之后,我再次进行了讨论,并且我解决了以下问题: root (正在运行此过程)没有X终端条目,对于诸如 notify之类的某些事物来说,这是必不可少的-send 或启动Gui程序,事件发生后仍然有进程终止,如前所述.
当插入USB设备时,以下命令将向终端发送通知并启动wxPython Gui程序.

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.

脚本:

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

通过定义DISPLAY,我们可以解决没有X词条的root问题.
通过将希望运行的命令与现在要运行的指令一起传递给 at 程序,可以避免udev杀死进程.

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.

/lib/udev/rules.d/10-usbinsert.rules文件:

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.

这篇关于如何在USB闪存驱动器插入上运行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆