5

I am trying to setup a system such that a script gets executed everytime any USB storage device is mounted (in this case, automounted by systemd). Based on a few references here, here and here, systemd allows for the execution of custom scripts after a specific device is mounted, but these either:

  • Need a specific device or mountpoint.
  • Use udev, which triggers too early, and holds the mounting process.
  • Use audits or logs, which isn't very satisfying.

Is there anyway to be less specific in systemd units, allowing for the use of ExecStart after any succesful (auto)mount?

  • another unsatisfying solution is udisksctl monitor which shows each disk being mounted. – meuh Jul 14 '17 at 16:20
  • Have your USB storage the same number of partition? – user192526 Jul 16 '17 at 7:32
  • I have no knowledge about the drive's structure. All I know is that partitions are going to get automounted, and the script should trigger for every one of them. I'd like the solution to stay generic like that. – John WH Smith Jul 17 '17 at 12:33
0

I can only poiting the way.

 cat /etc/udev/rules.d/bb-clam.rules 
KERNEL=="sd[b-z]*", SUBSYSTEMS=="usb", TAG+="systemd", ENV{SYSTEMD_WANTS}="clamtk.service"

this triggers my unit clamtk.service `cat /etc/systemd/system/clamtk.service

[Unit]
Description=clamtk 

Requires=dev-media0.device


[Service]
Type=simple 
User=alex
ExecStart=/bin/bash -c "/usr/local/bin/clam"
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=15s




[Install]
WantedBy=multiuser.target 

`

But this works only good if your unit is started as user, and the unit is triggerd also for usb-harddrives.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.