How to make umount wait, until a device is not busy?

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











up vote
2
down vote

favorite












I have a bash script file where I execute a bunch of commands.



#!/bin/bash
umount /media/hdd1
umount /media/hdd2
something1
something2


But since the comands later in the file work with the umounted HDD, I need to make sure the umount is successful before continuing.



I can of course check if the umount failed and just exit 1, but that is not ideal.



So basically, what I would like to do, is somehow make the umount command wait, until the device is not busy and then umount the HDD and continue executing the script.



It would thus work like this:



#!/bin/bash
umount /media/hdd1 # Device umounted without any problems continuing the script..
umount /media/hdd2 # Device is busy! Let's just sit around and wait until it isn't... let's say 5 minutes later whatever was accessing that HDD isn't anymore and the umount umounts the HDD and the script continues
something1
something2


Thank you.










share|improve this question





















  • Maybe the -l (ell) switch? See linux.die.net/man/8/umount
    – PerlDuck
    3 hours ago










  • I don't think the lazy umount option blocks the script from executing until it finishes
    – Askerman
    3 hours ago














up vote
2
down vote

favorite












I have a bash script file where I execute a bunch of commands.



#!/bin/bash
umount /media/hdd1
umount /media/hdd2
something1
something2


But since the comands later in the file work with the umounted HDD, I need to make sure the umount is successful before continuing.



I can of course check if the umount failed and just exit 1, but that is not ideal.



So basically, what I would like to do, is somehow make the umount command wait, until the device is not busy and then umount the HDD and continue executing the script.



It would thus work like this:



#!/bin/bash
umount /media/hdd1 # Device umounted without any problems continuing the script..
umount /media/hdd2 # Device is busy! Let's just sit around and wait until it isn't... let's say 5 minutes later whatever was accessing that HDD isn't anymore and the umount umounts the HDD and the script continues
something1
something2


Thank you.










share|improve this question





















  • Maybe the -l (ell) switch? See linux.die.net/man/8/umount
    – PerlDuck
    3 hours ago










  • I don't think the lazy umount option blocks the script from executing until it finishes
    – Askerman
    3 hours ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a bash script file where I execute a bunch of commands.



#!/bin/bash
umount /media/hdd1
umount /media/hdd2
something1
something2


But since the comands later in the file work with the umounted HDD, I need to make sure the umount is successful before continuing.



I can of course check if the umount failed and just exit 1, but that is not ideal.



So basically, what I would like to do, is somehow make the umount command wait, until the device is not busy and then umount the HDD and continue executing the script.



It would thus work like this:



#!/bin/bash
umount /media/hdd1 # Device umounted without any problems continuing the script..
umount /media/hdd2 # Device is busy! Let's just sit around and wait until it isn't... let's say 5 minutes later whatever was accessing that HDD isn't anymore and the umount umounts the HDD and the script continues
something1
something2


Thank you.










share|improve this question













I have a bash script file where I execute a bunch of commands.



#!/bin/bash
umount /media/hdd1
umount /media/hdd2
something1
something2


But since the comands later in the file work with the umounted HDD, I need to make sure the umount is successful before continuing.



I can of course check if the umount failed and just exit 1, but that is not ideal.



So basically, what I would like to do, is somehow make the umount command wait, until the device is not busy and then umount the HDD and continue executing the script.



It would thus work like this:



#!/bin/bash
umount /media/hdd1 # Device umounted without any problems continuing the script..
umount /media/hdd2 # Device is busy! Let's just sit around and wait until it isn't... let's say 5 minutes later whatever was accessing that HDD isn't anymore and the umount umounts the HDD and the script continues
something1
something2


Thank you.







bash mount umount






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 3 hours ago









Askerman

366513




366513











  • Maybe the -l (ell) switch? See linux.die.net/man/8/umount
    – PerlDuck
    3 hours ago










  • I don't think the lazy umount option blocks the script from executing until it finishes
    – Askerman
    3 hours ago
















  • Maybe the -l (ell) switch? See linux.die.net/man/8/umount
    – PerlDuck
    3 hours ago










  • I don't think the lazy umount option blocks the script from executing until it finishes
    – Askerman
    3 hours ago















Maybe the -l (ell) switch? See linux.die.net/man/8/umount
– PerlDuck
3 hours ago




Maybe the -l (ell) switch? See linux.die.net/man/8/umount
– PerlDuck
3 hours ago












I don't think the lazy umount option blocks the script from executing until it finishes
– Askerman
3 hours ago




I don't think the lazy umount option blocks the script from executing until it finishes
– Askerman
3 hours ago










1 Answer
1






active

oldest

votes

















up vote
2
down vote













I think the following script will do the job. It should be run with sudo (superuser permissions).



There is a function doer with a while loop, that checks with mountpoint if a device is mounted at the specified mount point, and if it is, tries to unmount it with umount. When the logical variable busy is false, the while loop is finished and the script can start 'doing some things'.



#!/bin/bash


function doer()

busy=true
while $busy
do
if mountpoint -q "$1"
then
umount "$1" 2> /dev/null
if [ $? -eq 0 ]
then
busy=false # umount successful
else
echo -n '.' # output to show that the script is alive
sleep 5 # 5 seconds for testing, modify to 300 seconds later on
fi
else
busy=false # not mounted
fi
done


########################

# main

########################

doer /media/hdd1
doer /media/hdd2

echo ''
echo 'doing something1'
echo 'doing something2'





share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "89"
    ;
    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: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2faskubuntu.com%2fquestions%2f1083624%2fhow-to-make-umount-wait-until-a-device-is-not-busy%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
    2
    down vote













    I think the following script will do the job. It should be run with sudo (superuser permissions).



    There is a function doer with a while loop, that checks with mountpoint if a device is mounted at the specified mount point, and if it is, tries to unmount it with umount. When the logical variable busy is false, the while loop is finished and the script can start 'doing some things'.



    #!/bin/bash


    function doer()

    busy=true
    while $busy
    do
    if mountpoint -q "$1"
    then
    umount "$1" 2> /dev/null
    if [ $? -eq 0 ]
    then
    busy=false # umount successful
    else
    echo -n '.' # output to show that the script is alive
    sleep 5 # 5 seconds for testing, modify to 300 seconds later on
    fi
    else
    busy=false # not mounted
    fi
    done


    ########################

    # main

    ########################

    doer /media/hdd1
    doer /media/hdd2

    echo ''
    echo 'doing something1'
    echo 'doing something2'





    share|improve this answer


























      up vote
      2
      down vote













      I think the following script will do the job. It should be run with sudo (superuser permissions).



      There is a function doer with a while loop, that checks with mountpoint if a device is mounted at the specified mount point, and if it is, tries to unmount it with umount. When the logical variable busy is false, the while loop is finished and the script can start 'doing some things'.



      #!/bin/bash


      function doer()

      busy=true
      while $busy
      do
      if mountpoint -q "$1"
      then
      umount "$1" 2> /dev/null
      if [ $? -eq 0 ]
      then
      busy=false # umount successful
      else
      echo -n '.' # output to show that the script is alive
      sleep 5 # 5 seconds for testing, modify to 300 seconds later on
      fi
      else
      busy=false # not mounted
      fi
      done


      ########################

      # main

      ########################

      doer /media/hdd1
      doer /media/hdd2

      echo ''
      echo 'doing something1'
      echo 'doing something2'





      share|improve this answer
























        up vote
        2
        down vote










        up vote
        2
        down vote









        I think the following script will do the job. It should be run with sudo (superuser permissions).



        There is a function doer with a while loop, that checks with mountpoint if a device is mounted at the specified mount point, and if it is, tries to unmount it with umount. When the logical variable busy is false, the while loop is finished and the script can start 'doing some things'.



        #!/bin/bash


        function doer()

        busy=true
        while $busy
        do
        if mountpoint -q "$1"
        then
        umount "$1" 2> /dev/null
        if [ $? -eq 0 ]
        then
        busy=false # umount successful
        else
        echo -n '.' # output to show that the script is alive
        sleep 5 # 5 seconds for testing, modify to 300 seconds later on
        fi
        else
        busy=false # not mounted
        fi
        done


        ########################

        # main

        ########################

        doer /media/hdd1
        doer /media/hdd2

        echo ''
        echo 'doing something1'
        echo 'doing something2'





        share|improve this answer














        I think the following script will do the job. It should be run with sudo (superuser permissions).



        There is a function doer with a while loop, that checks with mountpoint if a device is mounted at the specified mount point, and if it is, tries to unmount it with umount. When the logical variable busy is false, the while loop is finished and the script can start 'doing some things'.



        #!/bin/bash


        function doer()

        busy=true
        while $busy
        do
        if mountpoint -q "$1"
        then
        umount "$1" 2> /dev/null
        if [ $? -eq 0 ]
        then
        busy=false # umount successful
        else
        echo -n '.' # output to show that the script is alive
        sleep 5 # 5 seconds for testing, modify to 300 seconds later on
        fi
        else
        busy=false # not mounted
        fi
        done


        ########################

        # main

        ########################

        doer /media/hdd1
        doer /media/hdd2

        echo ''
        echo 'doing something1'
        echo 'doing something2'






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 11 mins ago

























        answered 2 hours ago









        sudodus

        20.7k32768




        20.7k32768



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1083624%2fhow-to-make-umount-wait-until-a-device-is-not-busy%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery