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"
내 시스템 서비스( /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
USB 드라이브가 연결되어 있는지 확인하면 /var/log/syslog
드라이브가 컴퓨터에서 인식되지만 내 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