AnswerBun.com

How to execute a script after every systemd automount?

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?

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

1 Answers

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!

Related Questions

How to expand “word” in bash? Not talking about file wildcards

2  Asked on October 31, 2021 by luis-santos

     

Parse a string using keywords

7  Asked on October 31, 2021 by beaker

       

Why does the command a-=2 fail?

4  Asked on October 31, 2021 by tmpbin

   

Shell dropping every other character of input

3  Asked on October 31, 2021 by michael-davis

         

Out of memory, but swap available

1  Asked on October 31, 2021

       

How can I display list of files in a directory?

4  Asked on October 31, 2021 by brocharvey

   

Booting from ISO suddenly stopped working

0  Asked on October 31, 2021 by ajgringo619

       

Does Pulseaudio leak memory?

2  Asked on October 31, 2021

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.