TransWikia.com

How to execute a script after every systemd automount?

Unix & Linux Asked by John WH Smith on December 27, 2020

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?

One Answer

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.

Answered by user192526 on December 27, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP