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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/589841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:42:53  来源:igfitidea点击:

Upstart calling script (for inserted USB-drive)

linuxbashdetectusb-driveupstart

提问by

I know that Ubuntu (and Fedora) uses Upstartistead 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. I would like Upstart to call my own script for this.

我想知道如何检测何时插入了 USB 驱动器,安装它并将一些文件复制到它。我希望 Upstart 为此调用我自己的脚本。

If it is possible I would like Upstart to call the script for a specificUSB-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? (As SysVinit seems not to.)

作为旁注,除了 Upstart 之外,您还知道其他任何系统吗?它以一种很好的方式处理 USB 驱动器、网络安装的文件系统等?(因为SysVinit 似乎没有。)

采纳答案by joeforker

upstartdoesn't seem to come with "usb device plugged in" signals out of the box. 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.

upstart似乎没有开箱即用的“插入 USB 设备”信号。到目前为止的重点是做与 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 那样运作,也将使我们能够开始向这种模型平稳过渡。”

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. You could also have udev call your backup script directly.

幸运的是,您可以通过让 udev 运行脚本来发送您的自定义 upstart 信号来自己实现未来,这样 upstart 就可以调用您的备份脚本。您也可以让 udev 直接调用您的备份脚本。

udevalready has a simple way to run scripts when devices are plugged and unplugged. See rename your usb hard drive's device name with udev rules. On my system, I would have to use udevadm monitor --envinstead of the tutorial's udevmonitor --env. After following the tutorial, you would create another udev rule like this one:

udev已经有了一种在设备插入和拔出时运行脚本的简单方法。请参阅使用 udev 规则重命名 USB 硬盘驱动器的设备名称。在我的系统上,我必须使用udevadm monitor --env而不是教程的udevmonitor --env. 遵循本教程后,您将创建另一个 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_SHORTwith your device's actual id, and $env{NAME}with whatever udev environment variable(s) your script needs to find the backup device. You might need to background the script to avoid blocking udev.

替换ID_SERIAL_SHORT为您的设备的实际 id,以及$env{NAME}您的脚本需要查找备份设备的任何 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.dbeginning with the line start on back-it-up.

如果你想使用的暴发户,你可以有你的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?for hints on doing the same with DBus. DBus might be more convenient if you want to have the logged in user run a usermode "watch for backup drive" daemon.

另请参阅如何在 Python 中侦听 Linux 中的“插入 USB 设备”事件?有关对DBus执行相同操作的提示。如果您想让登录用户运行用户模式“监视备份驱动器”守护程序,DBus 可能会更方便。

回答by deft_code

In Ubuntu 9.10 and newer Upstart has some udev capabilities through the upstart-udev-bridgeservice.

在 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. With a DBus solution you could pop up notifications to the user and provide easy user control.

我喜欢新贵的简单和优雅。但是,如果不那么优雅,DBus 解决方案可能会更好。使用 DBus 解决方案,您可以向用户弹出通知并提供简单的用户控制。