Terminate BASH For loop, that contains long sleep and other stuff, using any specific key












1















I have a bash script that




  • has a for loop that iterates a lot of times and sleeps between iterations, potentially for a long time. It then


  • writes results to a file, then terminates.



On occasion I get the result I need before many loop iterations are complete.



On these occasions I need the script to break out of the for loop while it is either




  • doing stuff, or


  • sleeping



in such as way that it will continue with the rest of the script, after the for loop, which is writing a report file of data it's gathered so far.



I wish to use a key combination, eg CTRL+Q, to break out of the for loop.



Script looks like this:



#!/bin/bash

for (( c=0; c<=$1; c++ ))
do

# SOME STUFF HERE
# data gathering into arrays and other commands here etc

# sleep potentially for a long time
sleep $2

done

#WRITE REPORT OUT TO SCREEN AND FILE HERE









share|improve this question





























    1















    I have a bash script that




    • has a for loop that iterates a lot of times and sleeps between iterations, potentially for a long time. It then


    • writes results to a file, then terminates.



    On occasion I get the result I need before many loop iterations are complete.



    On these occasions I need the script to break out of the for loop while it is either




    • doing stuff, or


    • sleeping



    in such as way that it will continue with the rest of the script, after the for loop, which is writing a report file of data it's gathered so far.



    I wish to use a key combination, eg CTRL+Q, to break out of the for loop.



    Script looks like this:



    #!/bin/bash

    for (( c=0; c<=$1; c++ ))
    do

    # SOME STUFF HERE
    # data gathering into arrays and other commands here etc

    # sleep potentially for a long time
    sleep $2

    done

    #WRITE REPORT OUT TO SCREEN AND FILE HERE









    share|improve this question



























      1












      1








      1








      I have a bash script that




      • has a for loop that iterates a lot of times and sleeps between iterations, potentially for a long time. It then


      • writes results to a file, then terminates.



      On occasion I get the result I need before many loop iterations are complete.



      On these occasions I need the script to break out of the for loop while it is either




      • doing stuff, or


      • sleeping



      in such as way that it will continue with the rest of the script, after the for loop, which is writing a report file of data it's gathered so far.



      I wish to use a key combination, eg CTRL+Q, to break out of the for loop.



      Script looks like this:



      #!/bin/bash

      for (( c=0; c<=$1; c++ ))
      do

      # SOME STUFF HERE
      # data gathering into arrays and other commands here etc

      # sleep potentially for a long time
      sleep $2

      done

      #WRITE REPORT OUT TO SCREEN AND FILE HERE









      share|improve this question
















      I have a bash script that




      • has a for loop that iterates a lot of times and sleeps between iterations, potentially for a long time. It then


      • writes results to a file, then terminates.



      On occasion I get the result I need before many loop iterations are complete.



      On these occasions I need the script to break out of the for loop while it is either




      • doing stuff, or


      • sleeping



      in such as way that it will continue with the rest of the script, after the for loop, which is writing a report file of data it's gathered so far.



      I wish to use a key combination, eg CTRL+Q, to break out of the for loop.



      Script looks like this:



      #!/bin/bash

      for (( c=0; c<=$1; c++ ))
      do

      # SOME STUFF HERE
      # data gathering into arrays and other commands here etc

      # sleep potentially for a long time
      sleep $2

      done

      #WRITE REPORT OUT TO SCREEN AND FILE HERE






      bash shell-script sleep






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 36 mins ago







      Kes

















      asked 51 mins ago









      KesKes

      816




      816






















          2 Answers
          2






          active

          oldest

          votes


















          2














          I've not had contact with this kind of tasks for a while, but I remember something like this used to work:



          #!/bin/bash

          trap break INT

          for (( c=0; c<=$1; c++ ))
          do

          # SOME STUFF HERE
          # data gathering into arrays and other commands here etc

          echo loop "$c" before sleep

          # sleep potentially for a long time
          sleep "$2"

          echo loop "$c" after sleep

          done

          #WRITE REPORT OUT TO SCREEN AND FILE HERE

          echo outside


          The idea is to use Ctrl-C to break the loop. This signal (SIGINT) is caught by the trap, which breaks the loop and lets the rest of the script follow.



          Example:



          $ ./s 3 1
          loop 0 before sleep
          loop 0 after sleep
          loop 1 before sleep
          ^Coutside


          Let me know if you have any problems with this.






          share|improve this answer
























          • Thank you @Tomasz, this works!

            – Kes
            13 mins ago



















          2














          The easiest way is to use Ctrl+C, since by default it gives the INT signal, which causes most applications to stop, and you can catch it in the shell.



          To interrupt the whole loop at once, you could run it in a subshell, and exit when interrupted. Here, the trap is set only in the subshell, and only it exits.



          #!/bin/bash

          (
          trap "echo interrupted.; exit 0" INT
          while true; do
          echo "running some task..."
          some heavy task
          echo "running sleep..."
          sleep 5;
          echo "iteration end."
          done
          )

          echo "script end."





          share|improve this answer
























          • Thanks @ilkkachu it does work.

            – Kes
            18 mins ago








          • 1





            @Kes, hmmh, I tested that on Debian with Bash 4.4. and Bash 5, and I'm not sure what would cause the difference. But if Tomasz's break solution works, then use that!

            – ilkkachu
            11 mins ago











          • Thanks @ilkkachu for your solution. It does work. It was my mistake. I forgot to use the shift to get the upper case C.

            – Kes
            9 mins ago











          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%2f496881%2fterminate-bash-for-loop-that-contains-long-sleep-and-other-stuff-using-any-spe%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









          2














          I've not had contact with this kind of tasks for a while, but I remember something like this used to work:



          #!/bin/bash

          trap break INT

          for (( c=0; c<=$1; c++ ))
          do

          # SOME STUFF HERE
          # data gathering into arrays and other commands here etc

          echo loop "$c" before sleep

          # sleep potentially for a long time
          sleep "$2"

          echo loop "$c" after sleep

          done

          #WRITE REPORT OUT TO SCREEN AND FILE HERE

          echo outside


          The idea is to use Ctrl-C to break the loop. This signal (SIGINT) is caught by the trap, which breaks the loop and lets the rest of the script follow.



          Example:



          $ ./s 3 1
          loop 0 before sleep
          loop 0 after sleep
          loop 1 before sleep
          ^Coutside


          Let me know if you have any problems with this.






          share|improve this answer
























          • Thank you @Tomasz, this works!

            – Kes
            13 mins ago
















          2














          I've not had contact with this kind of tasks for a while, but I remember something like this used to work:



          #!/bin/bash

          trap break INT

          for (( c=0; c<=$1; c++ ))
          do

          # SOME STUFF HERE
          # data gathering into arrays and other commands here etc

          echo loop "$c" before sleep

          # sleep potentially for a long time
          sleep "$2"

          echo loop "$c" after sleep

          done

          #WRITE REPORT OUT TO SCREEN AND FILE HERE

          echo outside


          The idea is to use Ctrl-C to break the loop. This signal (SIGINT) is caught by the trap, which breaks the loop and lets the rest of the script follow.



          Example:



          $ ./s 3 1
          loop 0 before sleep
          loop 0 after sleep
          loop 1 before sleep
          ^Coutside


          Let me know if you have any problems with this.






          share|improve this answer
























          • Thank you @Tomasz, this works!

            – Kes
            13 mins ago














          2












          2








          2







          I've not had contact with this kind of tasks for a while, but I remember something like this used to work:



          #!/bin/bash

          trap break INT

          for (( c=0; c<=$1; c++ ))
          do

          # SOME STUFF HERE
          # data gathering into arrays and other commands here etc

          echo loop "$c" before sleep

          # sleep potentially for a long time
          sleep "$2"

          echo loop "$c" after sleep

          done

          #WRITE REPORT OUT TO SCREEN AND FILE HERE

          echo outside


          The idea is to use Ctrl-C to break the loop. This signal (SIGINT) is caught by the trap, which breaks the loop and lets the rest of the script follow.



          Example:



          $ ./s 3 1
          loop 0 before sleep
          loop 0 after sleep
          loop 1 before sleep
          ^Coutside


          Let me know if you have any problems with this.






          share|improve this answer













          I've not had contact with this kind of tasks for a while, but I remember something like this used to work:



          #!/bin/bash

          trap break INT

          for (( c=0; c<=$1; c++ ))
          do

          # SOME STUFF HERE
          # data gathering into arrays and other commands here etc

          echo loop "$c" before sleep

          # sleep potentially for a long time
          sleep "$2"

          echo loop "$c" after sleep

          done

          #WRITE REPORT OUT TO SCREEN AND FILE HERE

          echo outside


          The idea is to use Ctrl-C to break the loop. This signal (SIGINT) is caught by the trap, which breaks the loop and lets the rest of the script follow.



          Example:



          $ ./s 3 1
          loop 0 before sleep
          loop 0 after sleep
          loop 1 before sleep
          ^Coutside


          Let me know if you have any problems with this.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 25 mins ago









          TomaszTomasz

          9,47152965




          9,47152965













          • Thank you @Tomasz, this works!

            – Kes
            13 mins ago



















          • Thank you @Tomasz, this works!

            – Kes
            13 mins ago

















          Thank you @Tomasz, this works!

          – Kes
          13 mins ago





          Thank you @Tomasz, this works!

          – Kes
          13 mins ago













          2














          The easiest way is to use Ctrl+C, since by default it gives the INT signal, which causes most applications to stop, and you can catch it in the shell.



          To interrupt the whole loop at once, you could run it in a subshell, and exit when interrupted. Here, the trap is set only in the subshell, and only it exits.



          #!/bin/bash

          (
          trap "echo interrupted.; exit 0" INT
          while true; do
          echo "running some task..."
          some heavy task
          echo "running sleep..."
          sleep 5;
          echo "iteration end."
          done
          )

          echo "script end."





          share|improve this answer
























          • Thanks @ilkkachu it does work.

            – Kes
            18 mins ago








          • 1





            @Kes, hmmh, I tested that on Debian with Bash 4.4. and Bash 5, and I'm not sure what would cause the difference. But if Tomasz's break solution works, then use that!

            – ilkkachu
            11 mins ago











          • Thanks @ilkkachu for your solution. It does work. It was my mistake. I forgot to use the shift to get the upper case C.

            – Kes
            9 mins ago
















          2














          The easiest way is to use Ctrl+C, since by default it gives the INT signal, which causes most applications to stop, and you can catch it in the shell.



          To interrupt the whole loop at once, you could run it in a subshell, and exit when interrupted. Here, the trap is set only in the subshell, and only it exits.



          #!/bin/bash

          (
          trap "echo interrupted.; exit 0" INT
          while true; do
          echo "running some task..."
          some heavy task
          echo "running sleep..."
          sleep 5;
          echo "iteration end."
          done
          )

          echo "script end."





          share|improve this answer
























          • Thanks @ilkkachu it does work.

            – Kes
            18 mins ago








          • 1





            @Kes, hmmh, I tested that on Debian with Bash 4.4. and Bash 5, and I'm not sure what would cause the difference. But if Tomasz's break solution works, then use that!

            – ilkkachu
            11 mins ago











          • Thanks @ilkkachu for your solution. It does work. It was my mistake. I forgot to use the shift to get the upper case C.

            – Kes
            9 mins ago














          2












          2








          2







          The easiest way is to use Ctrl+C, since by default it gives the INT signal, which causes most applications to stop, and you can catch it in the shell.



          To interrupt the whole loop at once, you could run it in a subshell, and exit when interrupted. Here, the trap is set only in the subshell, and only it exits.



          #!/bin/bash

          (
          trap "echo interrupted.; exit 0" INT
          while true; do
          echo "running some task..."
          some heavy task
          echo "running sleep..."
          sleep 5;
          echo "iteration end."
          done
          )

          echo "script end."





          share|improve this answer













          The easiest way is to use Ctrl+C, since by default it gives the INT signal, which causes most applications to stop, and you can catch it in the shell.



          To interrupt the whole loop at once, you could run it in a subshell, and exit when interrupted. Here, the trap is set only in the subshell, and only it exits.



          #!/bin/bash

          (
          trap "echo interrupted.; exit 0" INT
          while true; do
          echo "running some task..."
          some heavy task
          echo "running sleep..."
          sleep 5;
          echo "iteration end."
          done
          )

          echo "script end."






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 23 mins ago









          ilkkachuilkkachu

          57.1k785158




          57.1k785158













          • Thanks @ilkkachu it does work.

            – Kes
            18 mins ago








          • 1





            @Kes, hmmh, I tested that on Debian with Bash 4.4. and Bash 5, and I'm not sure what would cause the difference. But if Tomasz's break solution works, then use that!

            – ilkkachu
            11 mins ago











          • Thanks @ilkkachu for your solution. It does work. It was my mistake. I forgot to use the shift to get the upper case C.

            – Kes
            9 mins ago



















          • Thanks @ilkkachu it does work.

            – Kes
            18 mins ago








          • 1





            @Kes, hmmh, I tested that on Debian with Bash 4.4. and Bash 5, and I'm not sure what would cause the difference. But if Tomasz's break solution works, then use that!

            – ilkkachu
            11 mins ago











          • Thanks @ilkkachu for your solution. It does work. It was my mistake. I forgot to use the shift to get the upper case C.

            – Kes
            9 mins ago

















          Thanks @ilkkachu it does work.

          – Kes
          18 mins ago







          Thanks @ilkkachu it does work.

          – Kes
          18 mins ago






          1




          1





          @Kes, hmmh, I tested that on Debian with Bash 4.4. and Bash 5, and I'm not sure what would cause the difference. But if Tomasz's break solution works, then use that!

          – ilkkachu
          11 mins ago





          @Kes, hmmh, I tested that on Debian with Bash 4.4. and Bash 5, and I'm not sure what would cause the difference. But if Tomasz's break solution works, then use that!

          – ilkkachu
          11 mins ago













          Thanks @ilkkachu for your solution. It does work. It was my mistake. I forgot to use the shift to get the upper case C.

          – Kes
          9 mins ago





          Thanks @ilkkachu for your solution. It does work. It was my mistake. I forgot to use the shift to get the upper case C.

          – Kes
          9 mins ago


















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496881%2fterminate-bash-for-loop-that-contains-long-sleep-and-other-stuff-using-any-spe%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python