How to manage “device or resource busy” in a script?











up vote
0
down vote

favorite












I have a little script that have to remove some files.
How can I manage the device or resource busy error of rm?
Can I catch it? Like a try/catch?
So that if I catch it I, for example, sleep 3 seconds and then retry..



Thanks










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a little script that have to remove some files.
    How can I manage the device or resource busy error of rm?
    Can I catch it? Like a try/catch?
    So that if I catch it I, for example, sleep 3 seconds and then retry..



    Thanks










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a little script that have to remove some files.
      How can I manage the device or resource busy error of rm?
      Can I catch it? Like a try/catch?
      So that if I catch it I, for example, sleep 3 seconds and then retry..



      Thanks










      share|improve this question















      I have a little script that have to remove some files.
      How can I manage the device or resource busy error of rm?
      Can I catch it? Like a try/catch?
      So that if I catch it I, for example, sleep 3 seconds and then retry..



      Thanks







      shell-script rm






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 14 '17 at 13:00









      Ketan

      5,75842742




      5,75842742










      asked Jun 14 '17 at 12:29









      Cirelli94

      1011




      1011






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          If it's an error that you think will go away after some time, then you could try a simple loop:



          while ! rm some files; do
          echo 'rm failed, sleeping for 5 seconds'
          sleep 5
          echo 'retrying...'
          done





          share|improve this answer




























            up vote
            0
            down vote













            If you want to catch THAT specific error, try to probe the error code 16 on your last rm execution. Example:



            rm /path/to/foo
            if [ $? -eq 16 ]; then
            echo "Device busy"
            fi


            $? is the special shell variable that means "exit state of the last command". If this state is zero 0, means that the last command succeded, and if different than zero means failure and it maps to an error message. All error messages are available at Linux source file linux/include/uapi/asm-generic/errno-base.h, and 16 is:



            #define EBUSY       16  /* Device or resource busy */


            If you want to deal with the proccess that is causing the open file error lsof +D /path/too/foo/ will show what files are opened inside that directory. lsof(8) manpage.



            Related stuff:




            • Device or resource busy

            • How to get over “device or resource busy”?

            • Meaning of $? in shell scripts






            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',
              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%2f371074%2fhow-to-manage-device-or-resource-busy-in-a-script%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              If it's an error that you think will go away after some time, then you could try a simple loop:



              while ! rm some files; do
              echo 'rm failed, sleeping for 5 seconds'
              sleep 5
              echo 'retrying...'
              done





              share|improve this answer

























                up vote
                0
                down vote













                If it's an error that you think will go away after some time, then you could try a simple loop:



                while ! rm some files; do
                echo 'rm failed, sleeping for 5 seconds'
                sleep 5
                echo 'retrying...'
                done





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  If it's an error that you think will go away after some time, then you could try a simple loop:



                  while ! rm some files; do
                  echo 'rm failed, sleeping for 5 seconds'
                  sleep 5
                  echo 'retrying...'
                  done





                  share|improve this answer












                  If it's an error that you think will go away after some time, then you could try a simple loop:



                  while ! rm some files; do
                  echo 'rm failed, sleeping for 5 seconds'
                  sleep 5
                  echo 'retrying...'
                  done






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 14 '17 at 12:35









                  Kusalananda

                  119k16223364




                  119k16223364
























                      up vote
                      0
                      down vote













                      If you want to catch THAT specific error, try to probe the error code 16 on your last rm execution. Example:



                      rm /path/to/foo
                      if [ $? -eq 16 ]; then
                      echo "Device busy"
                      fi


                      $? is the special shell variable that means "exit state of the last command". If this state is zero 0, means that the last command succeded, and if different than zero means failure and it maps to an error message. All error messages are available at Linux source file linux/include/uapi/asm-generic/errno-base.h, and 16 is:



                      #define EBUSY       16  /* Device or resource busy */


                      If you want to deal with the proccess that is causing the open file error lsof +D /path/too/foo/ will show what files are opened inside that directory. lsof(8) manpage.



                      Related stuff:




                      • Device or resource busy

                      • How to get over “device or resource busy”?

                      • Meaning of $? in shell scripts






                      share|improve this answer



























                        up vote
                        0
                        down vote













                        If you want to catch THAT specific error, try to probe the error code 16 on your last rm execution. Example:



                        rm /path/to/foo
                        if [ $? -eq 16 ]; then
                        echo "Device busy"
                        fi


                        $? is the special shell variable that means "exit state of the last command". If this state is zero 0, means that the last command succeded, and if different than zero means failure and it maps to an error message. All error messages are available at Linux source file linux/include/uapi/asm-generic/errno-base.h, and 16 is:



                        #define EBUSY       16  /* Device or resource busy */


                        If you want to deal with the proccess that is causing the open file error lsof +D /path/too/foo/ will show what files are opened inside that directory. lsof(8) manpage.



                        Related stuff:




                        • Device or resource busy

                        • How to get over “device or resource busy”?

                        • Meaning of $? in shell scripts






                        share|improve this answer

























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          If you want to catch THAT specific error, try to probe the error code 16 on your last rm execution. Example:



                          rm /path/to/foo
                          if [ $? -eq 16 ]; then
                          echo "Device busy"
                          fi


                          $? is the special shell variable that means "exit state of the last command". If this state is zero 0, means that the last command succeded, and if different than zero means failure and it maps to an error message. All error messages are available at Linux source file linux/include/uapi/asm-generic/errno-base.h, and 16 is:



                          #define EBUSY       16  /* Device or resource busy */


                          If you want to deal with the proccess that is causing the open file error lsof +D /path/too/foo/ will show what files are opened inside that directory. lsof(8) manpage.



                          Related stuff:




                          • Device or resource busy

                          • How to get over “device or resource busy”?

                          • Meaning of $? in shell scripts






                          share|improve this answer














                          If you want to catch THAT specific error, try to probe the error code 16 on your last rm execution. Example:



                          rm /path/to/foo
                          if [ $? -eq 16 ]; then
                          echo "Device busy"
                          fi


                          $? is the special shell variable that means "exit state of the last command". If this state is zero 0, means that the last command succeded, and if different than zero means failure and it maps to an error message. All error messages are available at Linux source file linux/include/uapi/asm-generic/errno-base.h, and 16 is:



                          #define EBUSY       16  /* Device or resource busy */


                          If you want to deal with the proccess that is causing the open file error lsof +D /path/too/foo/ will show what files are opened inside that directory. lsof(8) manpage.



                          Related stuff:




                          • Device or resource busy

                          • How to get over “device or resource busy”?

                          • Meaning of $? in shell scripts







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jun 14 '17 at 12:56

























                          answered Jun 14 '17 at 12:39









                          nwildner

                          13.8k14075




                          13.8k14075






























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f371074%2fhow-to-manage-device-or-resource-busy-in-a-script%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

                              Morgemoulin

                              Scott Moir

                              Souastre