我试图在插入 USB 存储设备时从 udev 触发 systemd 服务,并将设备信息(名称)传递给我的服务,该服务会将其提供给脚本。
我创建了一个 udev 规则(/etc/udev/rules.d/99-foo.rules
):
ACTION=="add", KERNEL=="sd?", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", TAG+="systemd", PROGRAM="/bin/systemd-escape -p %k", ENV{SYSTEMD_WANTS}+="bar@%c.service"
我的systemd服务(/etc/systemd/system/bar.service
)是比较基础的:
[Unit]
Description=Script to do stuff with inserted device.
[Service]
Type=oneshot
ExecStart=/root/bin/usbhook %I
[Install]
WantedBy=multi-user.target
当我检查/var/log/syslog
USB 驱动器插入时,我看到机器识别的驱动器,但我的 systemd 服务未启动:
Jan 1 18:58:18 metapi kernel: [ 4542.190520] usb 1-1: New USB device found, idVendor=0781, idProduct=55a5, bcdDevice= 1.00
Jan 1 18:58:18 metapi kernel: [ 4542.190547] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Jan 1 18:58:18 metapi kernel: [ 4542.190560] usb 1-1: Product: Cruzer Snap
Jan 1 18:58:18 metapi kernel: [ 4542.190571] usb 1-1: Manufacturer: SanDisk
Jan 1 18:58:18 metapi kernel: [ 4542.190584] usb 1-1: SerialNumber: 4C530000180521234567
Jan 1 18:58:18 metapi kernel: [ 4542.211636] usb-storage 1-1:1.0: USB Mass Storage device detected
Jan 1 18:58:18 metapi kernel: [ 4542.225388] scsi host0: usb-storage 1-1:1.0
Jan 1 18:58:19 metapi mtp-probe: checking bus 1, device 22: "/sys/devices/platform/soc/20980000.usb/usb1/1-1"
Jan 1 18:58:19 metapi mtp-probe: bus: 1, device: 22 was not an MTP device
Jan 1 18:58:19 metapi mtp-probe: checking bus 1, device 22: "/sys/devices/platform/soc/20980000.usb/usb1/1-1"
Jan 1 18:58:19 metapi mtp-probe: bus: 1, device: 22 was not an MTP device
Jan 1 18:58:19 metapi kernel: [ 4543.230653] scsi 0:0:0:0: Direct-Access SanDisk Cruzer Snap 1.00 PQ: 0 ANSI: 6
Jan 1 18:58:19 metapi kernel: [ 4543.232528] sd 0:0:0:0: [sda] 250085376 512-byte logical blocks: (128 GB/119 GiB)
Jan 1 18:58:19 metapi kernel: [ 4543.234365] sd 0:0:0:0: [sda] Write Protect is off
Jan 1 18:58:19 metapi kernel: [ 4543.234392] sd 0:0:0:0: [sda] Mode Sense: 43 00 00 00
Jan 1 18:58:19 metapi kernel: [ 4543.235109] sd 0:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
Jan 1 18:58:19 metapi kernel: [ 4543.236616] sd 0:0:0:0: Attached scsi generic sg0 type 0
Jan 1 18:58:19 metapi kernel: [ 4543.266857] sda: sda1
Jan 1 18:58:20 metapi kernel: [ 4543.274096] sd 0:0:0:0: [sda] Attached SCSI removable disk
请注意,我相信我的 udev 规则和 systemd 脚本应该正常运行。如果我将 udev 规则替换为ACTION=="add", KERNEL=="sd?", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", TAG+="systemd", ENV{SYSTEMD_WANTS}+="bar.service"
,它不传递参数,则 systemd 脚本将启动:
...
Jan 1 19:03:27 metapi kernel: [ 4850.274286] sd 0:0:0:0: [sda] 250085376 512-byte logical blocks: (128 GB/119 GiB)
Jan 1 19:03:27 metapi kernel: [ 4850.275523] sd 0:0:0:0: Attached scsi generic sg0 type 0
Jan 1 19:03:27 metapi kernel: [ 4850.276057] sd 0:0:0:0: [sda] Write Protect is off
Jan 1 19:03:27 metapi kernel: [ 4850.276081] sd 0:0:0:0: [sda] Mode Sense: 43 00 00 00
Jan 1 19:03:27 metapi kernel: [ 4850.276840] sd 0:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
Jan 1 19:03:27 metapi kernel: [ 4850.309435] sda: sda1
Jan 1 19:03:27 metapi kernel: [ 4850.316819] sd 0:0:0:0: [sda] Attached SCSI removable disk
Jan 1 19:03:27 metapi systemd[1]: Starting Script to do stuff with inserted device....
答案1
这不是一个论点。服务没有论据。那是一个实例名称为一个模板单元……
……你没有。您正在尝试启动实例[email protected]
,但没有要[email protected]
实例化的模板服务单元。
答案2
我需要在服务名称中添加“@”。像这样:
sudo mv /etc/systemd/system/bar.service /etc/systemd/system/[email protected]
在本教程中,名称中还添加了一个“@”以传递参数:http://blog.fraggod.net/2012/06/16/proper-ish-way-to-start-long-running-systemd-service-on-udev-event-device-hotplug.html