简体   繁体   English

新贵调用脚本(用于插入的 USB 驱动器)

[英]Upstart calling script (for inserted USB-drive)

I know that Ubuntu (and Fedora) uses Upstart istead of the classical System V init daemon (SysVinit).我知道 Ubuntu(和 Fedora)使用Upstart而不是经典的 System V init 守护进程(SysVinit)。

I would like to know how to detect when a USB-drive has been inserted, mount it and copy some files to it.我想知道如何检测何时插入 USB 驱动器、挂载它并将一些文件复制到其中。 I would like Upstart to call my own script for this.我希望 Upstart 为此调用我自己的脚本。

If it is possible I would like Upstart to call the script for a specific USB-drive, so that I would get normal functionality for every USB-drive except my "instant backup" USB-drive.如果可能的话,我希望 Upstart 为特定的 USB 驱动器调用脚本,这样我就可以获得除“即时备份”USB 驱动器之外的每个 USB 驱动器的正常功能。

If Upstart could send the USB-drive identification string as an argument to my script I guess that would be the ideal solution, as I would be able to have the id string in my script and possibly could make the script handle two USB-drives without much change.如果 Upstart 可以将 USB 驱动器标识字符串作为参数发送到我的脚本,我想这将是理想的解决方案,因为我可以在脚本中包含 id 字符串,并且可能使脚本处理两个 USB 驱动器而无需变化很大。

And as a side-note, do you know any other system except Upstart which handles USB-drives, network mounted file systems and such in a nice way?顺便说一句,你知道除了 Upstart 之外的任何其他系统,它以一种很好的方式处理 USB 驱动器、网络安装的文件系统等? (As SysVinit seems not to .) (因为SysVinit 似乎不是。)

upstart doesn't seem to come with "usb device plugged in" signals out of the box. upstart似乎没有开箱即用的“usb device plugged in”信号。 The focus so far has been to do pretty much exactly the same thing as init, and the "cool advertised features" are in the future.到目前为止,重点一直是做与 init 几乎完全相同的事情,并且“很酷的广告功能”是在未来。

From the Fedora wiki : "...getting Upstart itself in place now, even though it will only be functioning as SysV does now, will allow us to begin a smooth transition toward this model."来自 Fedora wiki :“......现在让 Upstart 本身就位,即使它现在只能像 SysV 那样运行,也将使我们能够开始向这个 model 平稳过渡。”

Luckily, you can implement the future yourself by having udev run a script to send your custom upstart signal so upstart can call your backup script.幸运的是,您可以通过让 udev 运行脚本来发送您的自定义 upstart 信号来自己实现未来,以便 upstart 可以调用您的备份脚本。 You could also have udev call your backup script directly.您也可以让 udev 直接调用您的备份脚本。

udev already has a simple way to run scripts when devices are plugged and unplugged. udev已经有一种简单的方法来在设备插入和拔出时运行脚本。 See rename your usb hard drive's device name with udev rules .请参阅使用 udev 规则重命名您的 usb 硬盘驱动器的设备名称 On my system, I would have to use udevadm monitor --env instead of the tutorial's udevmonitor --env .在我的系统上,我必须使用udevadm monitor --env而不是教程的udevmonitor --env After following the tutorial, you would create another udev rule like this one:遵循本教程后,您将创建另一个像这样的 udev 规则:

echo 'SUBSYSTEM=="block", ID_SERIAL_SHORT=="101A9041C67D182E", \
NAME="myusbdrive", \
RUN+="/my/backup/script $env{NAME}"' > /etc/udev/rules.d/S96-mydrive.rules

Replacing ID_SERIAL_SHORT with your device's actual id, and $env{NAME} with whatever udev environment variable(s) your script needs to find the backup device.ID_SERIAL_SHORT替换为您设备的实际 ID,并将$env{NAME}替换为脚本查找备份设备所需的任何 udev 环境变量。 You might need to background the script to avoid blocking udev.您可能需要将脚本置于后台以避免阻塞 udev。

If you want to use upstart, you could have your udev rule run /sbin/initctl emit back-it-up VARIABLE=$env{VARIABLE}... and then write a script in /etc/event.d beginning with the line start on back-it-up .如果你想使用 upstart,你可以让你的 udev 规则运行/sbin/initctl emit back-it-up VARIABLE=$env{VARIABLE}...然后在/etc/event.d中以该行开头编写一个脚本start on back-it-up

See also How can I listen for 'usb device inserted' events in Linux, in Python?另请参阅如何在 Linux、Python 中侦听“插入 USB 设备”事件? for hints on doing the same with DBus .有关对DBus执行相同操作的提示。 DBus might be more convenient if you want to have the logged in user run a usermode "watch for backup drive" daemon.如果您想让登录的用户运行用户模式“监视备份驱动器”守护程序,DBus 可能会更方便。

In Ubuntu 9.10 and newer Upstart has some udev capabilities through the upstart-udev-bridge service.在 Ubuntu 9.10 和更新版本中,Upstart 通过upstart-udev-bridge服务具有一些 udev 功能。

#thumbdrive_special.conf
start on block-device-added

task

script
   if [ `blkid $DEV` -eq "YOUR-THUMBDRIVES-UUID" ]; then
      /home/you/bin/thumbdrive_special $DEV
   fi
end script

I love how simple and elegant upstart can be.我喜欢新贵的简单和优雅。 However, a DBus solution might be better if less elegant.但是,如果不太优雅,DBus 解决方案可能会更好。 With a DBus solution you could pop up notifications to the user and provide easy user control.使用 DBus 解决方案,您可以向用户弹出通知并提供简单的用户控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM