Start/stop a user unit whenever a device has been mounted/unmounted

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











up vote
1
down vote

favorite












I moved most of my biggest files to a removable disk, some of these files are also torrents that I would like to seed. I use deluged as my torrent client which is started by systemd on my user. Now, the drive may be plugged at boot time or mounted at later date, which of course causes deluged to be confused and complain that the files are a mismatch. The most naive solution is to only start deluged service after the device is mounted and that's what I plan to do.



I've seen these answers which suggest using RequiresMountsFor and according to systemctl --user show deluged seems to do what I want:



After=basic.target -.slice home.mount network-online.target media-braiam-AA64B45
RequiresMountsFor=/media/braiam/AA64B45A64B42AC9 /home/braiam


Now, that allows the unit to be started if I have mounted the device, but it doesn't stops the unit when the device is being unmounted, nor seems to start the service when the device is being mounted.



In summary:



  • The service starts only if the device is mounted or can be mounted

  • The service will stop if the device is being unmounted

  • The service will start if the device is mounted

  • The device will stay mounted if the service is stopped.

This is what I've managed:



[Unit]
Description=Deluge Bittorrent Client Daemon
After=network-online.target
RequiresMountsFor=/media/braiam/id/
BindsTo=media-braiam-id.mount


For now, this stops the service when it's being unmounted, starts only if the mount point can be mounted and tries to mount the device. It won't mount the device, since only root can mount it (I think if I install a unit for the system, this will be possible). It doesn't start if the mount point becomes available, however. How do I achieve what I want?










share|improve this question























  • Not sure. Description=Deluge Bittorrent Client Daemon RequiresMountsFor=/media/braiam/id/ After=media-braiam-id.mount
    – user192526
    Jan 8 '17 at 22:06















up vote
1
down vote

favorite












I moved most of my biggest files to a removable disk, some of these files are also torrents that I would like to seed. I use deluged as my torrent client which is started by systemd on my user. Now, the drive may be plugged at boot time or mounted at later date, which of course causes deluged to be confused and complain that the files are a mismatch. The most naive solution is to only start deluged service after the device is mounted and that's what I plan to do.



I've seen these answers which suggest using RequiresMountsFor and according to systemctl --user show deluged seems to do what I want:



After=basic.target -.slice home.mount network-online.target media-braiam-AA64B45
RequiresMountsFor=/media/braiam/AA64B45A64B42AC9 /home/braiam


Now, that allows the unit to be started if I have mounted the device, but it doesn't stops the unit when the device is being unmounted, nor seems to start the service when the device is being mounted.



In summary:



  • The service starts only if the device is mounted or can be mounted

  • The service will stop if the device is being unmounted

  • The service will start if the device is mounted

  • The device will stay mounted if the service is stopped.

This is what I've managed:



[Unit]
Description=Deluge Bittorrent Client Daemon
After=network-online.target
RequiresMountsFor=/media/braiam/id/
BindsTo=media-braiam-id.mount


For now, this stops the service when it's being unmounted, starts only if the mount point can be mounted and tries to mount the device. It won't mount the device, since only root can mount it (I think if I install a unit for the system, this will be possible). It doesn't start if the mount point becomes available, however. How do I achieve what I want?










share|improve this question























  • Not sure. Description=Deluge Bittorrent Client Daemon RequiresMountsFor=/media/braiam/id/ After=media-braiam-id.mount
    – user192526
    Jan 8 '17 at 22:06













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I moved most of my biggest files to a removable disk, some of these files are also torrents that I would like to seed. I use deluged as my torrent client which is started by systemd on my user. Now, the drive may be plugged at boot time or mounted at later date, which of course causes deluged to be confused and complain that the files are a mismatch. The most naive solution is to only start deluged service after the device is mounted and that's what I plan to do.



I've seen these answers which suggest using RequiresMountsFor and according to systemctl --user show deluged seems to do what I want:



After=basic.target -.slice home.mount network-online.target media-braiam-AA64B45
RequiresMountsFor=/media/braiam/AA64B45A64B42AC9 /home/braiam


Now, that allows the unit to be started if I have mounted the device, but it doesn't stops the unit when the device is being unmounted, nor seems to start the service when the device is being mounted.



In summary:



  • The service starts only if the device is mounted or can be mounted

  • The service will stop if the device is being unmounted

  • The service will start if the device is mounted

  • The device will stay mounted if the service is stopped.

This is what I've managed:



[Unit]
Description=Deluge Bittorrent Client Daemon
After=network-online.target
RequiresMountsFor=/media/braiam/id/
BindsTo=media-braiam-id.mount


For now, this stops the service when it's being unmounted, starts only if the mount point can be mounted and tries to mount the device. It won't mount the device, since only root can mount it (I think if I install a unit for the system, this will be possible). It doesn't start if the mount point becomes available, however. How do I achieve what I want?










share|improve this question















I moved most of my biggest files to a removable disk, some of these files are also torrents that I would like to seed. I use deluged as my torrent client which is started by systemd on my user. Now, the drive may be plugged at boot time or mounted at later date, which of course causes deluged to be confused and complain that the files are a mismatch. The most naive solution is to only start deluged service after the device is mounted and that's what I plan to do.



I've seen these answers which suggest using RequiresMountsFor and according to systemctl --user show deluged seems to do what I want:



After=basic.target -.slice home.mount network-online.target media-braiam-AA64B45
RequiresMountsFor=/media/braiam/AA64B45A64B42AC9 /home/braiam


Now, that allows the unit to be started if I have mounted the device, but it doesn't stops the unit when the device is being unmounted, nor seems to start the service when the device is being mounted.



In summary:



  • The service starts only if the device is mounted or can be mounted

  • The service will stop if the device is being unmounted

  • The service will start if the device is mounted

  • The device will stay mounted if the service is stopped.

This is what I've managed:



[Unit]
Description=Deluge Bittorrent Client Daemon
After=network-online.target
RequiresMountsFor=/media/braiam/id/
BindsTo=media-braiam-id.mount


For now, this stops the service when it's being unmounted, starts only if the mount point can be mounted and tries to mount the device. It won't mount the device, since only root can mount it (I think if I install a unit for the system, this will be possible). It doesn't start if the mount point becomes available, however. How do I achieve what I want?







systemd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 13 '17 at 12:36









Community♦

1




1










asked Jan 8 '17 at 17:48









Braiam

22.7k1972133




22.7k1972133











  • Not sure. Description=Deluge Bittorrent Client Daemon RequiresMountsFor=/media/braiam/id/ After=media-braiam-id.mount
    – user192526
    Jan 8 '17 at 22:06

















  • Not sure. Description=Deluge Bittorrent Client Daemon RequiresMountsFor=/media/braiam/id/ After=media-braiam-id.mount
    – user192526
    Jan 8 '17 at 22:06
















Not sure. Description=Deluge Bittorrent Client Daemon RequiresMountsFor=/media/braiam/id/ After=media-braiam-id.mount
– user192526
Jan 8 '17 at 22:06





Not sure. Description=Deluge Bittorrent Client Daemon RequiresMountsFor=/media/braiam/id/ After=media-braiam-id.mount
– user192526
Jan 8 '17 at 22:06











1 Answer
1






active

oldest

votes

















up vote
1
down vote













You need BindsTo= to start and stop both units together. But remember systemd tends to parallelize. You also need After= for the mount to be fully active before starting the service. It also has the opposite effect: the service must be fully stopped before the mount can be taken down.



[Unit]
Description=Deluge Bittorrent Client Daemon
After=network-online.target
BindsTo=media-braiam-id.mount
After=media-braiam-id.mount


Simlar question on stack overflow:
https://stackoverflow.com/questions/48687099/filesystem-are-unmounted-before-the-services-are-stopped-in-systemd/52882065#52882065





share




















    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f335812%2fstart-stop-a-user-unit-whenever-a-device-has-been-mounted-unmounted%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    You need BindsTo= to start and stop both units together. But remember systemd tends to parallelize. You also need After= for the mount to be fully active before starting the service. It also has the opposite effect: the service must be fully stopped before the mount can be taken down.



    [Unit]
    Description=Deluge Bittorrent Client Daemon
    After=network-online.target
    BindsTo=media-braiam-id.mount
    After=media-braiam-id.mount


    Simlar question on stack overflow:
    https://stackoverflow.com/questions/48687099/filesystem-are-unmounted-before-the-services-are-stopped-in-systemd/52882065#52882065





    share
























      up vote
      1
      down vote













      You need BindsTo= to start and stop both units together. But remember systemd tends to parallelize. You also need After= for the mount to be fully active before starting the service. It also has the opposite effect: the service must be fully stopped before the mount can be taken down.



      [Unit]
      Description=Deluge Bittorrent Client Daemon
      After=network-online.target
      BindsTo=media-braiam-id.mount
      After=media-braiam-id.mount


      Simlar question on stack overflow:
      https://stackoverflow.com/questions/48687099/filesystem-are-unmounted-before-the-services-are-stopped-in-systemd/52882065#52882065





      share






















        up vote
        1
        down vote










        up vote
        1
        down vote









        You need BindsTo= to start and stop both units together. But remember systemd tends to parallelize. You also need After= for the mount to be fully active before starting the service. It also has the opposite effect: the service must be fully stopped before the mount can be taken down.



        [Unit]
        Description=Deluge Bittorrent Client Daemon
        After=network-online.target
        BindsTo=media-braiam-id.mount
        After=media-braiam-id.mount


        Simlar question on stack overflow:
        https://stackoverflow.com/questions/48687099/filesystem-are-unmounted-before-the-services-are-stopped-in-systemd/52882065#52882065





        share












        You need BindsTo= to start and stop both units together. But remember systemd tends to parallelize. You also need After= for the mount to be fully active before starting the service. It also has the opposite effect: the service must be fully stopped before the mount can be taken down.



        [Unit]
        Description=Deluge Bittorrent Client Daemon
        After=network-online.target
        BindsTo=media-braiam-id.mount
        After=media-braiam-id.mount


        Simlar question on stack overflow:
        https://stackoverflow.com/questions/48687099/filesystem-are-unmounted-before-the-services-are-stopped-in-systemd/52882065#52882065






        share











        share


        share










        answered 2 mins ago









        Philippe A.

        1164




        1164



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f335812%2fstart-stop-a-user-unit-whenever-a-device-has-been-mounted-unmounted%23new-answer', 'question_page');

            );

            Post as a guest













































































            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