Wait for the completion of previous execution for shell script

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











up vote
1
down vote

favorite












I need to execute a shell script every hour. However I don't want to start the next run till the previous one is complete.



Please advise.







share|improve this question
























    up vote
    1
    down vote

    favorite












    I need to execute a shell script every hour. However I don't want to start the next run till the previous one is complete.



    Please advise.







    share|improve this question






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I need to execute a shell script every hour. However I don't want to start the next run till the previous one is complete.



      Please advise.







      share|improve this question












      I need to execute a shell script every hour. However I don't want to start the next run till the previous one is complete.



      Please advise.









      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 4 '17 at 15:06









      puneet gupta

      211




      211




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote













          Since you tagged the question as Linux, you can use flock to put a lock on a well-known file:



          flock "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options


          A second instance of the command (run the same way) will wait until the first instance has finished. The problem with this approach is, if the command regularly takes more than an hour you'll just accumulate more and more instances waiting for the lock.



          Alternatively, you can just skip running a second instance if the first hasn't finished:



          flock -n "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options


          The problem with this approach is you may run the command fewer that 24 times per day.






          share|improve this answer



























            up vote
            1
            down vote













            You need to define your problem better.



            Sato has described how a method where new instances will back up behind a delayed one.



            Jaroslav's solution only provides a mechanism for detecting that the previous instance has not yet finished cleanly.



            Another option would be to schedule the next instance of the script inside the script itself:



            at now + 1 hour $*


            You need to define the behaviour if the previous instance has not completed, and whether you want instances to start at hourly intervals or within a defined interval between completion and start.






            share|improve this answer




















            • Thanks, Sato solution is solving my problem. Mostly my jobs will complete in an hour. However if not then I wants to wait for certain time.
              – puneet gupta
              Nov 4 '17 at 18:11

















            up vote
            0
            down vote













            You can create some temporary file on script start and delete it before script close. The script should check for existence of that file. If it finds it, it'd just close and do nothing. If it doesn't, create it and continue.



            Or in the cron job, you can use pgrep for checking the script being executed and continue only if it doesn't.






            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: 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%2f402511%2fwait-for-the-completion-of-previous-execution-for-shell-script%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote













              Since you tagged the question as Linux, you can use flock to put a lock on a well-known file:



              flock "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options


              A second instance of the command (run the same way) will wait until the first instance has finished. The problem with this approach is, if the command regularly takes more than an hour you'll just accumulate more and more instances waiting for the lock.



              Alternatively, you can just skip running a second instance if the first hasn't finished:



              flock -n "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options


              The problem with this approach is you may run the command fewer that 24 times per day.






              share|improve this answer
























                up vote
                2
                down vote













                Since you tagged the question as Linux, you can use flock to put a lock on a well-known file:



                flock "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options


                A second instance of the command (run the same way) will wait until the first instance has finished. The problem with this approach is, if the command regularly takes more than an hour you'll just accumulate more and more instances waiting for the lock.



                Alternatively, you can just skip running a second instance if the first hasn't finished:



                flock -n "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options


                The problem with this approach is you may run the command fewer that 24 times per day.






                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Since you tagged the question as Linux, you can use flock to put a lock on a well-known file:



                  flock "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options


                  A second instance of the command (run the same way) will wait until the first instance has finished. The problem with this approach is, if the command regularly takes more than an hour you'll just accumulate more and more instances waiting for the lock.



                  Alternatively, you can just skip running a second instance if the first hasn't finished:



                  flock -n "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options


                  The problem with this approach is you may run the command fewer that 24 times per day.






                  share|improve this answer












                  Since you tagged the question as Linux, you can use flock to put a lock on a well-known file:



                  flock "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options


                  A second instance of the command (run the same way) will wait until the first instance has finished. The problem with this approach is, if the command regularly takes more than an hour you'll just accumulate more and more instances waiting for the lock.



                  Alternatively, you can just skip running a second instance if the first hasn't finished:



                  flock -n "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options


                  The problem with this approach is you may run the command fewer that 24 times per day.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 4 '17 at 15:33









                  Satō Katsura

                  10.7k11533




                  10.7k11533






















                      up vote
                      1
                      down vote













                      You need to define your problem better.



                      Sato has described how a method where new instances will back up behind a delayed one.



                      Jaroslav's solution only provides a mechanism for detecting that the previous instance has not yet finished cleanly.



                      Another option would be to schedule the next instance of the script inside the script itself:



                      at now + 1 hour $*


                      You need to define the behaviour if the previous instance has not completed, and whether you want instances to start at hourly intervals or within a defined interval between completion and start.






                      share|improve this answer




















                      • Thanks, Sato solution is solving my problem. Mostly my jobs will complete in an hour. However if not then I wants to wait for certain time.
                        – puneet gupta
                        Nov 4 '17 at 18:11














                      up vote
                      1
                      down vote













                      You need to define your problem better.



                      Sato has described how a method where new instances will back up behind a delayed one.



                      Jaroslav's solution only provides a mechanism for detecting that the previous instance has not yet finished cleanly.



                      Another option would be to schedule the next instance of the script inside the script itself:



                      at now + 1 hour $*


                      You need to define the behaviour if the previous instance has not completed, and whether you want instances to start at hourly intervals or within a defined interval between completion and start.






                      share|improve this answer




















                      • Thanks, Sato solution is solving my problem. Mostly my jobs will complete in an hour. However if not then I wants to wait for certain time.
                        – puneet gupta
                        Nov 4 '17 at 18:11












                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      You need to define your problem better.



                      Sato has described how a method where new instances will back up behind a delayed one.



                      Jaroslav's solution only provides a mechanism for detecting that the previous instance has not yet finished cleanly.



                      Another option would be to schedule the next instance of the script inside the script itself:



                      at now + 1 hour $*


                      You need to define the behaviour if the previous instance has not completed, and whether you want instances to start at hourly intervals or within a defined interval between completion and start.






                      share|improve this answer












                      You need to define your problem better.



                      Sato has described how a method where new instances will back up behind a delayed one.



                      Jaroslav's solution only provides a mechanism for detecting that the previous instance has not yet finished cleanly.



                      Another option would be to schedule the next instance of the script inside the script itself:



                      at now + 1 hour $*


                      You need to define the behaviour if the previous instance has not completed, and whether you want instances to start at hourly intervals or within a defined interval between completion and start.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 4 '17 at 16:16









                      symcbean

                      2,23911121




                      2,23911121











                      • Thanks, Sato solution is solving my problem. Mostly my jobs will complete in an hour. However if not then I wants to wait for certain time.
                        – puneet gupta
                        Nov 4 '17 at 18:11
















                      • Thanks, Sato solution is solving my problem. Mostly my jobs will complete in an hour. However if not then I wants to wait for certain time.
                        – puneet gupta
                        Nov 4 '17 at 18:11















                      Thanks, Sato solution is solving my problem. Mostly my jobs will complete in an hour. However if not then I wants to wait for certain time.
                      – puneet gupta
                      Nov 4 '17 at 18:11




                      Thanks, Sato solution is solving my problem. Mostly my jobs will complete in an hour. However if not then I wants to wait for certain time.
                      – puneet gupta
                      Nov 4 '17 at 18:11










                      up vote
                      0
                      down vote













                      You can create some temporary file on script start and delete it before script close. The script should check for existence of that file. If it finds it, it'd just close and do nothing. If it doesn't, create it and continue.



                      Or in the cron job, you can use pgrep for checking the script being executed and continue only if it doesn't.






                      share|improve this answer
























                        up vote
                        0
                        down vote













                        You can create some temporary file on script start and delete it before script close. The script should check for existence of that file. If it finds it, it'd just close and do nothing. If it doesn't, create it and continue.



                        Or in the cron job, you can use pgrep for checking the script being executed and continue only if it doesn't.






                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You can create some temporary file on script start and delete it before script close. The script should check for existence of that file. If it finds it, it'd just close and do nothing. If it doesn't, create it and continue.



                          Or in the cron job, you can use pgrep for checking the script being executed and continue only if it doesn't.






                          share|improve this answer












                          You can create some temporary file on script start and delete it before script close. The script should check for existence of that file. If it finds it, it'd just close and do nothing. If it doesn't, create it and continue.



                          Or in the cron job, you can use pgrep for checking the script being executed and continue only if it doesn't.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 4 '17 at 15:18









                          Jaroslav Kucera

                          4,3754621




                          4,3754621



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402511%2fwait-for-the-completion-of-previous-execution-for-shell-script%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