Upstart calling script (for inserted USB-drive)

前端 未结 2 680
旧巷少年郎
旧巷少年郎 2021-02-02 03:05

I know that Ubuntu (and Fedora) uses Upstart istead of the classical System V init daemon (SysVinit).

I would like to know how to detect when a USB-drive has been insert

2条回答
  •  遥遥无期
    2021-02-02 03:47

    upstart doesn'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.

    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."

    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 already 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 --env instead of the tutorial's udevmonitor --env. After following the tutorial, you would create another udev rule like this one:

    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. You might need to background the script to avoid blocking 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.

    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.

提交回复
热议问题