linux bash prevention flocking for background process












0














My script makes flock. This prevents me for runing its twice.
Script can execute another script, and there is a problem.
Sometimes another scripts leaves processes in background. Unfortunately they inherits flocks, I don't need this.
Is it any wraper who close some file handles before execute specified executable?



I use linux debian, but it isn't impornant linux distro or version.



Detailed scenario:




  1. My script A is run

  2. My script A makes flock

  3. My script A runs external executable B

  4. External B runs executable C as background (inherit flock)

  5. External B finishes and leaves C in background (inherit flock)

  6. My script A finishes because B finishes

  7. Another loop, My script A is run (as point 1)

  8. My script A makes flock and fail, because C still is running


I want exactly run external executable B with closed flocked filehandle. Flock should be keeped until only my script A is running. I don't want close flock between 2 and 3 points, because there will be "flock hole". Is there any wrapper os something like this?










share|improve this question













migrated from serverfault.com Aug 11 '14 at 21:13


This question came from our site for system and network administrators.















  • How do you make flock with bash?
    – peterh
    Aug 4 '14 at 12:18










  • @PeterHorvath Probably like this.
    – Michael Hampton
    Aug 4 '14 at 12:30










  • It's not important how. by flock shell command for example: stackoverflow.com/questions/169964/…. Under step " # Do stuff" all commands inherits flocked handles, this is POSIX behavior. I found here stackoverflow.com/questions/13171663/… some workaround, this should be template for my wrapper. But I'd preffer found some executable ready to use :)
    – Znik
    Aug 4 '14 at 14:24
















0














My script makes flock. This prevents me for runing its twice.
Script can execute another script, and there is a problem.
Sometimes another scripts leaves processes in background. Unfortunately they inherits flocks, I don't need this.
Is it any wraper who close some file handles before execute specified executable?



I use linux debian, but it isn't impornant linux distro or version.



Detailed scenario:




  1. My script A is run

  2. My script A makes flock

  3. My script A runs external executable B

  4. External B runs executable C as background (inherit flock)

  5. External B finishes and leaves C in background (inherit flock)

  6. My script A finishes because B finishes

  7. Another loop, My script A is run (as point 1)

  8. My script A makes flock and fail, because C still is running


I want exactly run external executable B with closed flocked filehandle. Flock should be keeped until only my script A is running. I don't want close flock between 2 and 3 points, because there will be "flock hole". Is there any wrapper os something like this?










share|improve this question













migrated from serverfault.com Aug 11 '14 at 21:13


This question came from our site for system and network administrators.















  • How do you make flock with bash?
    – peterh
    Aug 4 '14 at 12:18










  • @PeterHorvath Probably like this.
    – Michael Hampton
    Aug 4 '14 at 12:30










  • It's not important how. by flock shell command for example: stackoverflow.com/questions/169964/…. Under step " # Do stuff" all commands inherits flocked handles, this is POSIX behavior. I found here stackoverflow.com/questions/13171663/… some workaround, this should be template for my wrapper. But I'd preffer found some executable ready to use :)
    – Znik
    Aug 4 '14 at 14:24














0












0








0







My script makes flock. This prevents me for runing its twice.
Script can execute another script, and there is a problem.
Sometimes another scripts leaves processes in background. Unfortunately they inherits flocks, I don't need this.
Is it any wraper who close some file handles before execute specified executable?



I use linux debian, but it isn't impornant linux distro or version.



Detailed scenario:




  1. My script A is run

  2. My script A makes flock

  3. My script A runs external executable B

  4. External B runs executable C as background (inherit flock)

  5. External B finishes and leaves C in background (inherit flock)

  6. My script A finishes because B finishes

  7. Another loop, My script A is run (as point 1)

  8. My script A makes flock and fail, because C still is running


I want exactly run external executable B with closed flocked filehandle. Flock should be keeped until only my script A is running. I don't want close flock between 2 and 3 points, because there will be "flock hole". Is there any wrapper os something like this?










share|improve this question













My script makes flock. This prevents me for runing its twice.
Script can execute another script, and there is a problem.
Sometimes another scripts leaves processes in background. Unfortunately they inherits flocks, I don't need this.
Is it any wraper who close some file handles before execute specified executable?



I use linux debian, but it isn't impornant linux distro or version.



Detailed scenario:




  1. My script A is run

  2. My script A makes flock

  3. My script A runs external executable B

  4. External B runs executable C as background (inherit flock)

  5. External B finishes and leaves C in background (inherit flock)

  6. My script A finishes because B finishes

  7. Another loop, My script A is run (as point 1)

  8. My script A makes flock and fail, because C still is running


I want exactly run external executable B with closed flocked filehandle. Flock should be keeped until only my script A is running. I don't want close flock between 2 and 3 points, because there will be "flock hole". Is there any wrapper os something like this?







linux






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 4 '14 at 10:26









ZnikZnik

253129




253129




migrated from serverfault.com Aug 11 '14 at 21:13


This question came from our site for system and network administrators.






migrated from serverfault.com Aug 11 '14 at 21:13


This question came from our site for system and network administrators.














  • How do you make flock with bash?
    – peterh
    Aug 4 '14 at 12:18










  • @PeterHorvath Probably like this.
    – Michael Hampton
    Aug 4 '14 at 12:30










  • It's not important how. by flock shell command for example: stackoverflow.com/questions/169964/…. Under step " # Do stuff" all commands inherits flocked handles, this is POSIX behavior. I found here stackoverflow.com/questions/13171663/… some workaround, this should be template for my wrapper. But I'd preffer found some executable ready to use :)
    – Znik
    Aug 4 '14 at 14:24


















  • How do you make flock with bash?
    – peterh
    Aug 4 '14 at 12:18










  • @PeterHorvath Probably like this.
    – Michael Hampton
    Aug 4 '14 at 12:30










  • It's not important how. by flock shell command for example: stackoverflow.com/questions/169964/…. Under step " # Do stuff" all commands inherits flocked handles, this is POSIX behavior. I found here stackoverflow.com/questions/13171663/… some workaround, this should be template for my wrapper. But I'd preffer found some executable ready to use :)
    – Znik
    Aug 4 '14 at 14:24
















How do you make flock with bash?
– peterh
Aug 4 '14 at 12:18




How do you make flock with bash?
– peterh
Aug 4 '14 at 12:18












@PeterHorvath Probably like this.
– Michael Hampton
Aug 4 '14 at 12:30




@PeterHorvath Probably like this.
– Michael Hampton
Aug 4 '14 at 12:30












It's not important how. by flock shell command for example: stackoverflow.com/questions/169964/…. Under step " # Do stuff" all commands inherits flocked handles, this is POSIX behavior. I found here stackoverflow.com/questions/13171663/… some workaround, this should be template for my wrapper. But I'd preffer found some executable ready to use :)
– Znik
Aug 4 '14 at 14:24




It's not important how. by flock shell command for example: stackoverflow.com/questions/169964/…. Under step " # Do stuff" all commands inherits flocked handles, this is POSIX behavior. I found here stackoverflow.com/questions/13171663/… some workaround, this should be template for my wrapper. But I'd preffer found some executable ready to use :)
– Znik
Aug 4 '14 at 14:24










3 Answers
3






active

oldest

votes


















1














According to flock's man page, this is one use of the -u or --unlock option:




-u, --unlock




Drop a lock. This is usually not required, since a lock is automatically dropped when the file is closed. However, it may be required in special cases, for example if the enclosed command group may have forked a background process which should not be holding the lock.





The prescribed technique is therefore something like this:



(
flock 200 | exit 99
# some commands that should be guarded by the lock
# some of them fork daemons that keep our lock file open
flock --unlock 200 # we're done with the lock, we can release it
) 200>/path/to/lock-file





share|improve this answer





















  • You're right. When process forks or run another process, it inherits flock with his parent. All processes in child tree should finish for leave flock. When script goes into non critical phase and it will take long time, flock should be freed. It is related to called commands also. when that commands are independed to main process, flock should be freed as in situation described before.
    – Znik
    Apr 20 '16 at 12:16



















0














I wrote some wrapper yesterday. It closes all unneeded handles, then call specified command. Unfortunately it doesn't check handle type, closes all escept some known handles.



<==== file: wrapper.sh ====>
#!/bin/bash
for fd in $(ls /proc/$$/fd); do
case "$fd" in
0|1|2|255)
;;
*)
eval "exec $fd>&-"
;;
esac
done
exec $1 $2 $3 $4 $5 $6 $7 $8 $9


you cal this by:



wrapper.sh some_command command parameters


I can use this warapper at step 3 on my call scenario. Then flock exists inside main script only, and wrapper breaks inheritation in called script and his children.






share|improve this answer





























    0














    The following code acquires the lock and the trap ensures it is released when the script exits. The child processes inherit the lock but do not keep it acquired after flock --unlock 200 executes.



    flock 200 | exit 1
    trap "flock --unlock 200" EXIT
    # The rest of the script...


    Note: Only 1 trap may be in effect at a time. If you need to execute multiple commands in an exit trap, then you need to put them in a method or combine them as such trap "flock --unlock 200; echo Bye" EXIT.






    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%2f149767%2flinux-bash-prevention-flocking-for-background-process%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      According to flock's man page, this is one use of the -u or --unlock option:




      -u, --unlock




      Drop a lock. This is usually not required, since a lock is automatically dropped when the file is closed. However, it may be required in special cases, for example if the enclosed command group may have forked a background process which should not be holding the lock.





      The prescribed technique is therefore something like this:



      (
      flock 200 | exit 99
      # some commands that should be guarded by the lock
      # some of them fork daemons that keep our lock file open
      flock --unlock 200 # we're done with the lock, we can release it
      ) 200>/path/to/lock-file





      share|improve this answer





















      • You're right. When process forks or run another process, it inherits flock with his parent. All processes in child tree should finish for leave flock. When script goes into non critical phase and it will take long time, flock should be freed. It is related to called commands also. when that commands are independed to main process, flock should be freed as in situation described before.
        – Znik
        Apr 20 '16 at 12:16
















      1














      According to flock's man page, this is one use of the -u or --unlock option:




      -u, --unlock




      Drop a lock. This is usually not required, since a lock is automatically dropped when the file is closed. However, it may be required in special cases, for example if the enclosed command group may have forked a background process which should not be holding the lock.





      The prescribed technique is therefore something like this:



      (
      flock 200 | exit 99
      # some commands that should be guarded by the lock
      # some of them fork daemons that keep our lock file open
      flock --unlock 200 # we're done with the lock, we can release it
      ) 200>/path/to/lock-file





      share|improve this answer





















      • You're right. When process forks or run another process, it inherits flock with his parent. All processes in child tree should finish for leave flock. When script goes into non critical phase and it will take long time, flock should be freed. It is related to called commands also. when that commands are independed to main process, flock should be freed as in situation described before.
        – Znik
        Apr 20 '16 at 12:16














      1












      1








      1






      According to flock's man page, this is one use of the -u or --unlock option:




      -u, --unlock




      Drop a lock. This is usually not required, since a lock is automatically dropped when the file is closed. However, it may be required in special cases, for example if the enclosed command group may have forked a background process which should not be holding the lock.





      The prescribed technique is therefore something like this:



      (
      flock 200 | exit 99
      # some commands that should be guarded by the lock
      # some of them fork daemons that keep our lock file open
      flock --unlock 200 # we're done with the lock, we can release it
      ) 200>/path/to/lock-file





      share|improve this answer












      According to flock's man page, this is one use of the -u or --unlock option:




      -u, --unlock




      Drop a lock. This is usually not required, since a lock is automatically dropped when the file is closed. However, it may be required in special cases, for example if the enclosed command group may have forked a background process which should not be holding the lock.





      The prescribed technique is therefore something like this:



      (
      flock 200 | exit 99
      # some commands that should be guarded by the lock
      # some of them fork daemons that keep our lock file open
      flock --unlock 200 # we're done with the lock, we can release it
      ) 200>/path/to/lock-file






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 30 '16 at 8:31









      P DaddyP Daddy

      20518




      20518












      • You're right. When process forks or run another process, it inherits flock with his parent. All processes in child tree should finish for leave flock. When script goes into non critical phase and it will take long time, flock should be freed. It is related to called commands also. when that commands are independed to main process, flock should be freed as in situation described before.
        – Znik
        Apr 20 '16 at 12:16


















      • You're right. When process forks or run another process, it inherits flock with his parent. All processes in child tree should finish for leave flock. When script goes into non critical phase and it will take long time, flock should be freed. It is related to called commands also. when that commands are independed to main process, flock should be freed as in situation described before.
        – Znik
        Apr 20 '16 at 12:16
















      You're right. When process forks or run another process, it inherits flock with his parent. All processes in child tree should finish for leave flock. When script goes into non critical phase and it will take long time, flock should be freed. It is related to called commands also. when that commands are independed to main process, flock should be freed as in situation described before.
      – Znik
      Apr 20 '16 at 12:16




      You're right. When process forks or run another process, it inherits flock with his parent. All processes in child tree should finish for leave flock. When script goes into non critical phase and it will take long time, flock should be freed. It is related to called commands also. when that commands are independed to main process, flock should be freed as in situation described before.
      – Znik
      Apr 20 '16 at 12:16













      0














      I wrote some wrapper yesterday. It closes all unneeded handles, then call specified command. Unfortunately it doesn't check handle type, closes all escept some known handles.



      <==== file: wrapper.sh ====>
      #!/bin/bash
      for fd in $(ls /proc/$$/fd); do
      case "$fd" in
      0|1|2|255)
      ;;
      *)
      eval "exec $fd>&-"
      ;;
      esac
      done
      exec $1 $2 $3 $4 $5 $6 $7 $8 $9


      you cal this by:



      wrapper.sh some_command command parameters


      I can use this warapper at step 3 on my call scenario. Then flock exists inside main script only, and wrapper breaks inheritation in called script and his children.






      share|improve this answer


























        0














        I wrote some wrapper yesterday. It closes all unneeded handles, then call specified command. Unfortunately it doesn't check handle type, closes all escept some known handles.



        <==== file: wrapper.sh ====>
        #!/bin/bash
        for fd in $(ls /proc/$$/fd); do
        case "$fd" in
        0|1|2|255)
        ;;
        *)
        eval "exec $fd>&-"
        ;;
        esac
        done
        exec $1 $2 $3 $4 $5 $6 $7 $8 $9


        you cal this by:



        wrapper.sh some_command command parameters


        I can use this warapper at step 3 on my call scenario. Then flock exists inside main script only, and wrapper breaks inheritation in called script and his children.






        share|improve this answer
























          0












          0








          0






          I wrote some wrapper yesterday. It closes all unneeded handles, then call specified command. Unfortunately it doesn't check handle type, closes all escept some known handles.



          <==== file: wrapper.sh ====>
          #!/bin/bash
          for fd in $(ls /proc/$$/fd); do
          case "$fd" in
          0|1|2|255)
          ;;
          *)
          eval "exec $fd>&-"
          ;;
          esac
          done
          exec $1 $2 $3 $4 $5 $6 $7 $8 $9


          you cal this by:



          wrapper.sh some_command command parameters


          I can use this warapper at step 3 on my call scenario. Then flock exists inside main script only, and wrapper breaks inheritation in called script and his children.






          share|improve this answer












          I wrote some wrapper yesterday. It closes all unneeded handles, then call specified command. Unfortunately it doesn't check handle type, closes all escept some known handles.



          <==== file: wrapper.sh ====>
          #!/bin/bash
          for fd in $(ls /proc/$$/fd); do
          case "$fd" in
          0|1|2|255)
          ;;
          *)
          eval "exec $fd>&-"
          ;;
          esac
          done
          exec $1 $2 $3 $4 $5 $6 $7 $8 $9


          you cal this by:



          wrapper.sh some_command command parameters


          I can use this warapper at step 3 on my call scenario. Then flock exists inside main script only, and wrapper breaks inheritation in called script and his children.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 5 '14 at 7:03









          ZnikZnik

          253129




          253129























              0














              The following code acquires the lock and the trap ensures it is released when the script exits. The child processes inherit the lock but do not keep it acquired after flock --unlock 200 executes.



              flock 200 | exit 1
              trap "flock --unlock 200" EXIT
              # The rest of the script...


              Note: Only 1 trap may be in effect at a time. If you need to execute multiple commands in an exit trap, then you need to put them in a method or combine them as such trap "flock --unlock 200; echo Bye" EXIT.






              share|improve this answer


























                0














                The following code acquires the lock and the trap ensures it is released when the script exits. The child processes inherit the lock but do not keep it acquired after flock --unlock 200 executes.



                flock 200 | exit 1
                trap "flock --unlock 200" EXIT
                # The rest of the script...


                Note: Only 1 trap may be in effect at a time. If you need to execute multiple commands in an exit trap, then you need to put them in a method or combine them as such trap "flock --unlock 200; echo Bye" EXIT.






                share|improve this answer
























                  0












                  0








                  0






                  The following code acquires the lock and the trap ensures it is released when the script exits. The child processes inherit the lock but do not keep it acquired after flock --unlock 200 executes.



                  flock 200 | exit 1
                  trap "flock --unlock 200" EXIT
                  # The rest of the script...


                  Note: Only 1 trap may be in effect at a time. If you need to execute multiple commands in an exit trap, then you need to put them in a method or combine them as such trap "flock --unlock 200; echo Bye" EXIT.






                  share|improve this answer












                  The following code acquires the lock and the trap ensures it is released when the script exits. The child processes inherit the lock but do not keep it acquired after flock --unlock 200 executes.



                  flock 200 | exit 1
                  trap "flock --unlock 200" EXIT
                  # The rest of the script...


                  Note: Only 1 trap may be in effect at a time. If you need to execute multiple commands in an exit trap, then you need to put them in a method or combine them as such trap "flock --unlock 200; echo Bye" EXIT.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 29 '18 at 20:20









                  NathanNathan

                  1116




                  1116






























                      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%2f149767%2flinux-bash-prevention-flocking-for-background-process%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

                      List directoties down one level, excluding some named directories and files

                      list processes belonging to a network namespace

                      list systemd RuntimeDirectory mounts