As we saw in [learning udev](./learning_udev.md) we can **run** scripts or commands when certain conditions are met in regards to physical device changes.
The *classic* way to run long running actions, such as a backup, was to have `udev` trigger a script and be done with it.
This behaviour is currently discouraged and will most likely **fail** if your script take too long.
The `man udev` pages state the following.
```
Starting daemons or other long-running processes is not allowed; the forked processes, detached or not, will be
unconditionally killed after the event handling has finished. In order to activate long-running processes from udev
rules, provide a service unit and pull it in from a udev device using the SYSTEMD_WANTS device property. See
systemd.device(5) for details.
```
It's pretty clear what you can't do, but how to do it *right* is less clear.
The idea is to start a systemd service when a device is plugged in.
Udev will tell systemd to start the service but will delegate the responsibility to systemd so the request is non-blocking.
## Writing the service
We start by writing a small script that serves as a placeholder to prove our workflow.
It will just echo a line to `journalctl`.
No need to manage our own logging systemd as we can take full advantage of systemd's built in logging.
Aug 23 22:41:48 deathstar sudo[965130]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=1000)
Aug 23 22:41:58 deathstar kernel: usb 3-1.5: new high-speed USB device number 60 using ehci-pci
Aug 23 22:41:58 deathstar kernel: usb 3-1.5: New USB device found, idVendor=0781, idProduct=5591, bcdDevice= 1.00
Aug 23 22:41:58 deathstar kernel: usb 3-1.5: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Aug 23 22:41:58 deathstar kernel: usb 3-1.5: Product: SanDisk 3.2Gen1
Aug 23 22:41:58 deathstar kernel: usb 3-1.5: Manufacturer: USB
Aug 23 22:41:58 deathstar kernel: usb 3-1.5: SerialNumber: 04018a57f24e4dc0135c1f4666b5f2ca4e8e15e65eedfd157b0146cf1427157522b000000000000000000000a03e3c7e008cb3189155810739aa4777
Aug 23 22:41:58 deathstar kernel: usb-storage 3-1.5:1.0: USB Mass Storage device detected
Aug 23 22:41:58 deathstar kernel: scsi host7: usb-storage 3-1.5:1.0
Aug 23 22:41:59 deathstar kernel: scsi 7:0:0:0: Direct-Access USB SanDisk 3.2Gen1 1.00 PQ: 0 ANSI: 6
Aug 23 22:41:59 deathstar kernel: sd 7:0:0:0: Attached scsi generic sg3 type 0
Aug 23 23:04:40 deathstar sudo[969830]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=1000)
Aug 23 23:04:45 deathstar kernel: usb 3-1.5: new high-speed USB device number 67 using ehci-pci
Aug 23 23:04:45 deathstar kernel: usb 3-1.5: New USB device found, idVendor=0781, idProduct=5591, bcdDevice= 1.00
Aug 23 23:04:45 deathstar kernel: usb 3-1.5: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Aug 23 23:04:45 deathstar kernel: usb 3-1.5: Product: SanDisk 3.2Gen1
Aug 23 23:04:45 deathstar kernel: usb 3-1.5: Manufacturer: USB
Aug 23 23:04:45 deathstar kernel: usb 3-1.5: SerialNumber: 04018a57f24e4dc0135c1f4666b5f2ca4e8e15e65eedfd157b0146cf1427157522b000000000000000000000a03e3c7e008cb3189155810739aa4777
Aug 23 23:04:45 deathstar kernel: usb-storage 3-1.5:1.0: USB Mass Storage device detected
Aug 23 23:04:45 deathstar kernel: scsi host8: usb-storage 3-1.5:1.0
Aug 23 23:04:46 deathstar kernel: scsi 8:0:0:0: Direct-Access USB SanDisk 3.2Gen1 1.00 PQ: 0 ANSI: 6
Aug 23 23:04:46 deathstar kernel: sd 8:0:0:0: Attached scsi generic sg3 type 0
Aug 23 23:04:46 deathstar kernel: sd 8:0:0:0: [sdf] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
Aug 23 23:04:46 deathstar kernel: sdf: sdf1
Aug 23 23:04:46 deathstar kernel: sd 8:0:0:0: [sdf] Attached SCSI removable disk
Aug 23 23:04:46 deathstar systemd[1]: Started Our own backup script.
Aug 23 23:04:46 deathstar bash[969865]: running as root will backup to sdf1
Aug 23 23:04:46 deathstar bash[969865]: created /media/backup_usb_stick
Aug 23 23:04:46 deathstar bash[969865]: mounted sdf1
Aug 23 23:04:46 deathstar kernel: EXT4-fs (sdf1): mounted filesystem with ordered data mode. Opts: (null)
Aug 23 23:04:46 deathstar bash[969876]: sending incremental file list
Aug 23 23:04:46 deathstar bash[969876]: created directory /media/backup_usb_stick/backups
Aug 23 23:04:46 deathstar bash[969876]: ./
Aug 23 23:04:46 deathstar bash[969876]: arm (Meshed).stl
Aug 23 23:04:46 deathstar bash[969876]: base_pivot (Meshed).stl
Aug 23 23:04:46 deathstar bash[969876]: base_pivot_alt (Meshed).stl
Aug 23 23:04:46 deathstar bash[969876]: base_plate (Meshed).stl
Aug 23 23:04:46 deathstar bash[969876]: base_plate_alt (Meshed).stl
Aug 23 23:04:46 deathstar bash[969876]: filament_guide.FCStd
Aug 23 23:04:46 deathstar bash[969876]: filament_guide.FCStd1
Aug 23 23:04:46 deathstar bash[969876]: filament_guide.stl
Aug 23 23:04:46 deathstar bash[969876]: ivar_riser.FCStd
Aug 23 23:04:46 deathstar bash[969876]: ivar_riser.FCStd1
Aug 23 23:04:46 deathstar bash[969876]: ivar_riser.stl
Aug 23 23:04:46 deathstar bash[969876]: light_pot_office (Meshed).stl
Aug 23 23:04:46 deathstar bash[969876]: light_pot_office.FCStd
Aug 23 23:04:46 deathstar bash[969876]: light_pot_office.FCStd1
Aug 23 23:04:46 deathstar bash[969876]: monitor_arm.FCStd
Aug 23 23:04:46 deathstar bash[969876]: monitor_arm.FCStd1
Aug 23 23:04:46 deathstar bash[969876]: monitor_arm_120mm.stl
Aug 23 23:04:46 deathstar bash[969876]: picam.FCStd
Aug 23 23:04:46 deathstar bash[969876]: picam.FCStd1
Aug 23 23:04:46 deathstar bash[969876]: vat_filter.FCStd
Aug 23 23:04:46 deathstar bash[969876]: vat_filter.FCStd1
Aug 23 23:04:46 deathstar bash[969876]: vat_filter.stl
Aug 23 23:04:46 deathstar bash[969876]: vesa_plate (Meshed).stl
Aug 23 23:04:46 deathstar bash[969876]: sent 1,849,171 bytes received 510 bytes 3,699,362.00 bytes/sec
Aug 23 23:04:46 deathstar bash[969876]: total size is 1,847,019 speedup is 1.00
Aug 23 23:04:46 deathstar bash[969865]: all done
Aug 23 23:04:46 deathstar bash[969865]: unmounted...
Aug 23 23:04:46 deathstar systemd[1]: backup@sdf1.service: Succeeded.
Aug 23 23:05:01 deathstar kernel: usb 3-1.5: USB disconnect, device number 67
Aug 23 23:05:04 deathstar kernel: usb 3-1.5: new high-speed USB device number 68 using ehci-pci
Aug 23 23:05:04 deathstar kernel: usb 3-1.5: New USB device found, idVendor=0781, idProduct=5591, bcdDevice= 1.00
Aug 23 23:05:04 deathstar kernel: usb 3-1.5: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Aug 23 23:05:04 deathstar kernel: usb 3-1.5: Product: SanDisk 3.2Gen1
Aug 23 23:05:04 deathstar kernel: usb 3-1.5: Manufacturer: USB
Aug 23 23:05:04 deathstar kernel: usb 3-1.5: SerialNumber: 04018a57f24e4dc0135c1f4666b5f2ca4e8e15e65eedfd157b0146cf1427157522b000000000000000000000a03e3c7e008cb3189155810739aa4777
Aug 23 23:05:04 deathstar kernel: usb-storage 3-1.5:1.0: USB Mass Storage device detected
Aug 23 23:05:04 deathstar kernel: scsi host8: usb-storage 3-1.5:1.0
Aug 23 23:05:05 deathstar kernel: scsi 8:0:0:0: Direct-Access USB SanDisk 3.2Gen1 1.00 PQ: 0 ANSI: 6
Aug 23 23:05:05 deathstar kernel: sd 8:0:0:0: Attached scsi generic sg3 type 0