How to execute a script after every systemd automount?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












2















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?










share|improve this question






















  • 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
















2















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?










share|improve this question






















  • 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














2












2








2








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?










share|improve this question














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?







mount systemd automounting






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 14 '17 at 14:37









John WH SmithJohn WH Smith

10.1k34254




10.1k34254












  • 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


















  • 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

















another unsatisfying solution is udisksctl monitor which shows each disk being mounted.

– meuh
Jul 14 '17 at 16:20





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





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






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











1 Answer
1






active

oldest

votes


















0














I can only poiting the way.



 cat /etc/udev/rules.d/bb-clam.rules 
KERNEL=="sd[b-z]*", SUBSYSTEMS=="usb", TAG+="systemd", ENVSYSTEMD_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.






share|improve this answer






















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f378486%2fhow-to-execute-a-script-after-every-systemd-automount%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I can only poiting the way.



     cat /etc/udev/rules.d/bb-clam.rules 
    KERNEL=="sd[b-z]*", SUBSYSTEMS=="usb", TAG+="systemd", ENVSYSTEMD_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.






    share|improve this answer



























      0














      I can only poiting the way.



       cat /etc/udev/rules.d/bb-clam.rules 
      KERNEL=="sd[b-z]*", SUBSYSTEMS=="usb", TAG+="systemd", ENVSYSTEMD_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.






      share|improve this answer

























        0












        0








        0







        I can only poiting the way.



         cat /etc/udev/rules.d/bb-clam.rules 
        KERNEL=="sd[b-z]*", SUBSYSTEMS=="usb", TAG+="systemd", ENVSYSTEMD_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.






        share|improve this answer













        I can only poiting the way.



         cat /etc/udev/rules.d/bb-clam.rules 
        KERNEL=="sd[b-z]*", SUBSYSTEMS=="usb", TAG+="systemd", ENVSYSTEMD_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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 30 '17 at 10:55







        user192526


































            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f378486%2fhow-to-execute-a-script-after-every-systemd-automount%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay