3 ms·
Note if the author reads this (perfect is the enemy of good, feel free to ignore): You can make udev run systemd services directly, replacing RUN=... by e.g. TA
by evgpbfhnr 4y ago
Note if the author reads this (perfect is the enemy of good, feel free to ignore): You can make udev run systemd services directly, replacing RUN=... by e.g. TAG+="systemd", ENV{SYSTEMD_WANTS}+="foo.service"
That might require you also remove the 'enabled = true' for the service, and make sure gphoto2 dies when the camera is unplugged?
Anyway, it was a nice read :)
- revilotom 4y agoWow thanks for the suggestion! I knew there must be some way to do it with fewer steps but didn't quite figure out what it was so I really appreciate this comment. When I get a chance I'll test what you said out!
- Filligree 4y agoMake sure it doesn't end up stopping the service on system activation, if you do that.
- yrro 4y agoIs BindsTo= the right way to have systemd shut the service down if the device disappears?
- evgpbfhnr 4y agoIt's been a while but yes BindsTo is a good idea if you can express the device in a reliable way. Unfortunately for USB devices the path will look like something that depends on the USB port it's been plugged in (I only have a sound card on my hands, but something like these two alternatives on two different ports): sys-devices-pci0000:00-0000:00:14.0-usb1-1\x2d4-1\x2d4:1.0-sound-card1-controlC1.device sys-devices-pci0000:00-0000:00:14.0-usb1-1\x2d1-1\x2d1:1.0-sound-card1-controlC1.device notice the usb-`1-1:4` became `1-1:1` That makes BindsTo unpractical unless you're sure you'll always plug in the camera on the same port everytime, so it might not be best. This made me look up how I'd do this if the needs arise, so I haven't tried, but `StopWhenUnneeded=true` in the service definition looks like it would stop it properly when the device "wanting" it is gone? cf. https://unix.stackexchange.com/questions/528803/systemd-doesnt-stop-the-service-when-the-device-is-removed https://unix.stackexchange.com/questions/528803/systemd-does... I learned something new :)
- ilyt 4y agoHuh, I was solving similar problem some time ago. I needed to run some pulseaudio commands (to split stereo input into 2 mono ones) after USB device was reconnected and my solution was RUN script adding a systemd timer (the commands had to be ran after pulseaudio registered new device) that just ran it few seconds after device was connected
- l0b0 4y agoUsing your suggestion I ended up with the following for a Canon 7D: services.udev.extraRules = '' ACTION=="add", \ ATTR{idVendor}=="04a9", \ ATTR{idProduct}=="319a", \ ENV{SYSTEMD_WANTS}+="external_webcam.service", \ SUBSYSTEM=="usb", \ TAG+="systemd" ''; systemd.services.external_webcam = { enable = true; script = '' ${pkgs.gphoto2}/bin/gphoto2 --stdout --capture-movie | ${pkgs.ffmpeg}/bin/ffmpeg -i - -vcodec rawvideo -pix_fmt yuv420p -f v4l2 /dev/video0 ''; wantedBy = ["multi-user.target"]; }; Worked first try! [1] [1] https://gitlab.com/engmark/root/-/commit/ae59aad118e6effcf47ca47698b4640008ccace8 https://gitlab.com/engmark/root/-/commit/ae59aad118e6effcf47...