Display Spinner while waiting for some process to finish












4















How can I show spinner till command line finish it is job? In other words, If I am running a script and I want to show spinner while this script is running and the spinner disappears when the script finish it is job.



Bellow is a common spinner code:



i=1
sp="/-|"
echo -n ' '
while true
do
printf "b${sp:i++%${#sp}:1}"
done


How can I link the previous spinner code to a command to let it show spinner while the command is running and the spinner disappears when the command finish it is job? If I include the command inside the loop it will loop with the spinner so what is the solution in this case?










share|improve this question



























    4















    How can I show spinner till command line finish it is job? In other words, If I am running a script and I want to show spinner while this script is running and the spinner disappears when the script finish it is job.



    Bellow is a common spinner code:



    i=1
    sp="/-|"
    echo -n ' '
    while true
    do
    printf "b${sp:i++%${#sp}:1}"
    done


    How can I link the previous spinner code to a command to let it show spinner while the command is running and the spinner disappears when the command finish it is job? If I include the command inside the loop it will loop with the spinner so what is the solution in this case?










    share|improve this question

























      4












      4








      4


      4






      How can I show spinner till command line finish it is job? In other words, If I am running a script and I want to show spinner while this script is running and the spinner disappears when the script finish it is job.



      Bellow is a common spinner code:



      i=1
      sp="/-|"
      echo -n ' '
      while true
      do
      printf "b${sp:i++%${#sp}:1}"
      done


      How can I link the previous spinner code to a command to let it show spinner while the command is running and the spinner disappears when the command finish it is job? If I include the command inside the loop it will loop with the spinner so what is the solution in this case?










      share|improve this question














      How can I show spinner till command line finish it is job? In other words, If I am running a script and I want to show spinner while this script is running and the spinner disappears when the script finish it is job.



      Bellow is a common spinner code:



      i=1
      sp="/-|"
      echo -n ' '
      while true
      do
      printf "b${sp:i++%${#sp}:1}"
      done


      How can I link the previous spinner code to a command to let it show spinner while the command is running and the spinner disappears when the command finish it is job? If I include the command inside the loop it will loop with the spinner so what is the solution in this case?







      bash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 24 '15 at 17:03







      user88036





























          3 Answers
          3






          active

          oldest

          votes


















          10














          Have your while loop watch for your real command to exit. I'll assume a Linux environment that has /proc entries for each PID, but you could slice it other ways:



          #!/bin/bash
          # your real command here, instead of sleep
          sleep 7 &
          PID=$!
          i=1
          sp="/-|"
          echo -n ' '
          while [ -d /proc/$PID ]
          do
          printf "b${sp:i++%${#sp}:1}"
          done





          share|improve this answer



















          • 3





            This is a busy loop that will eat up cpu resources. I'd suggest having a delay of some kind in your while loop.

            – ACase
            Jul 20 '16 at 14:36



















          7














          This shell script should do what you're looking for:



          #!/usr/bin/env bash

          show_spinner()
          {
          local -r pid="${1}"
          local -r delay='0.75'
          local spinstr='|/-'
          local temp
          while ps a | awk '{print $1}' | grep -q "${pid}"; do
          temp="${spinstr#?}"
          printf " [%c] " "${spinstr}"
          spinstr=${temp}${spinstr%"${temp}"}
          sleep "${delay}"
          printf "bbbbbb"
          done
          printf " bbbb"
          }

          ("$@") &
          show_spinner "$!"


          You can invoke it like this to display a spinner while the command sleep 10 is running:



          $ spinner sleep 10





          share|improve this answer
























          • See also stackoverflow.com/a/20369590/2908724

            – bishop
            Oct 21 '16 at 17:28



















          0














          Some creativity while waiting.



          #!/bin/bash
          # b.nelissen

          # spinner demo code

          # stepsize is the number of characters per animation step
          # frames is the frame-art
          # stepsize=1; frames='⌜⌝⌟⌞'
          # stepsize=1; frames='🕐🕜🕑🕝🕒🕞🕓🕟🕔🕠🕕🕡🕖🕢🕗🕣🕘🕤🕙🕥🕚🕦🕛🕧'
          # stepsize=1; frames='⛔️ '
          stepsize=12; frames=' 🎾 | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 / | 🎾/ | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 | '

          # the loop
          while true; do
          s=$(( (s+$stepsize) %${#frames} ));
          printf "r${frames:$s:$stepsize}"
          sleep 0.2
          done


          Animated gif:




          tennisball







          share|improve this answer


























          • Good ideas, but since your post is not an answer in itself I think you should either elaborate until it's one, or edit a previous answer and put your code in that.

            – PetaspeedBeaver
            5 hours 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%2f225179%2fdisplay-spinner-while-waiting-for-some-process-to-finish%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









          10














          Have your while loop watch for your real command to exit. I'll assume a Linux environment that has /proc entries for each PID, but you could slice it other ways:



          #!/bin/bash
          # your real command here, instead of sleep
          sleep 7 &
          PID=$!
          i=1
          sp="/-|"
          echo -n ' '
          while [ -d /proc/$PID ]
          do
          printf "b${sp:i++%${#sp}:1}"
          done





          share|improve this answer



















          • 3





            This is a busy loop that will eat up cpu resources. I'd suggest having a delay of some kind in your while loop.

            – ACase
            Jul 20 '16 at 14:36
















          10














          Have your while loop watch for your real command to exit. I'll assume a Linux environment that has /proc entries for each PID, but you could slice it other ways:



          #!/bin/bash
          # your real command here, instead of sleep
          sleep 7 &
          PID=$!
          i=1
          sp="/-|"
          echo -n ' '
          while [ -d /proc/$PID ]
          do
          printf "b${sp:i++%${#sp}:1}"
          done





          share|improve this answer



















          • 3





            This is a busy loop that will eat up cpu resources. I'd suggest having a delay of some kind in your while loop.

            – ACase
            Jul 20 '16 at 14:36














          10












          10








          10







          Have your while loop watch for your real command to exit. I'll assume a Linux environment that has /proc entries for each PID, but you could slice it other ways:



          #!/bin/bash
          # your real command here, instead of sleep
          sleep 7 &
          PID=$!
          i=1
          sp="/-|"
          echo -n ' '
          while [ -d /proc/$PID ]
          do
          printf "b${sp:i++%${#sp}:1}"
          done





          share|improve this answer













          Have your while loop watch for your real command to exit. I'll assume a Linux environment that has /proc entries for each PID, but you could slice it other ways:



          #!/bin/bash
          # your real command here, instead of sleep
          sleep 7 &
          PID=$!
          i=1
          sp="/-|"
          echo -n ' '
          while [ -d /proc/$PID ]
          do
          printf "b${sp:i++%${#sp}:1}"
          done






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 24 '15 at 17:22









          Jeff SchallerJeff Schaller

          39.7k1054126




          39.7k1054126








          • 3





            This is a busy loop that will eat up cpu resources. I'd suggest having a delay of some kind in your while loop.

            – ACase
            Jul 20 '16 at 14:36














          • 3





            This is a busy loop that will eat up cpu resources. I'd suggest having a delay of some kind in your while loop.

            – ACase
            Jul 20 '16 at 14:36








          3




          3





          This is a busy loop that will eat up cpu resources. I'd suggest having a delay of some kind in your while loop.

          – ACase
          Jul 20 '16 at 14:36





          This is a busy loop that will eat up cpu resources. I'd suggest having a delay of some kind in your while loop.

          – ACase
          Jul 20 '16 at 14:36













          7














          This shell script should do what you're looking for:



          #!/usr/bin/env bash

          show_spinner()
          {
          local -r pid="${1}"
          local -r delay='0.75'
          local spinstr='|/-'
          local temp
          while ps a | awk '{print $1}' | grep -q "${pid}"; do
          temp="${spinstr#?}"
          printf " [%c] " "${spinstr}"
          spinstr=${temp}${spinstr%"${temp}"}
          sleep "${delay}"
          printf "bbbbbb"
          done
          printf " bbbb"
          }

          ("$@") &
          show_spinner "$!"


          You can invoke it like this to display a spinner while the command sleep 10 is running:



          $ spinner sleep 10





          share|improve this answer
























          • See also stackoverflow.com/a/20369590/2908724

            – bishop
            Oct 21 '16 at 17:28
















          7














          This shell script should do what you're looking for:



          #!/usr/bin/env bash

          show_spinner()
          {
          local -r pid="${1}"
          local -r delay='0.75'
          local spinstr='|/-'
          local temp
          while ps a | awk '{print $1}' | grep -q "${pid}"; do
          temp="${spinstr#?}"
          printf " [%c] " "${spinstr}"
          spinstr=${temp}${spinstr%"${temp}"}
          sleep "${delay}"
          printf "bbbbbb"
          done
          printf " bbbb"
          }

          ("$@") &
          show_spinner "$!"


          You can invoke it like this to display a spinner while the command sleep 10 is running:



          $ spinner sleep 10





          share|improve this answer
























          • See also stackoverflow.com/a/20369590/2908724

            – bishop
            Oct 21 '16 at 17:28














          7












          7








          7







          This shell script should do what you're looking for:



          #!/usr/bin/env bash

          show_spinner()
          {
          local -r pid="${1}"
          local -r delay='0.75'
          local spinstr='|/-'
          local temp
          while ps a | awk '{print $1}' | grep -q "${pid}"; do
          temp="${spinstr#?}"
          printf " [%c] " "${spinstr}"
          spinstr=${temp}${spinstr%"${temp}"}
          sleep "${delay}"
          printf "bbbbbb"
          done
          printf " bbbb"
          }

          ("$@") &
          show_spinner "$!"


          You can invoke it like this to display a spinner while the command sleep 10 is running:



          $ spinner sleep 10





          share|improve this answer













          This shell script should do what you're looking for:



          #!/usr/bin/env bash

          show_spinner()
          {
          local -r pid="${1}"
          local -r delay='0.75'
          local spinstr='|/-'
          local temp
          while ps a | awk '{print $1}' | grep -q "${pid}"; do
          temp="${spinstr#?}"
          printf " [%c] " "${spinstr}"
          spinstr=${temp}${spinstr%"${temp}"}
          sleep "${delay}"
          printf "bbbbbb"
          done
          printf " bbbb"
          }

          ("$@") &
          show_spinner "$!"


          You can invoke it like this to display a spinner while the command sleep 10 is running:



          $ spinner sleep 10






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 14 '16 at 21:03









          jsearsjsears

          17114




          17114













          • See also stackoverflow.com/a/20369590/2908724

            – bishop
            Oct 21 '16 at 17:28



















          • See also stackoverflow.com/a/20369590/2908724

            – bishop
            Oct 21 '16 at 17:28

















          See also stackoverflow.com/a/20369590/2908724

          – bishop
          Oct 21 '16 at 17:28





          See also stackoverflow.com/a/20369590/2908724

          – bishop
          Oct 21 '16 at 17:28











          0














          Some creativity while waiting.



          #!/bin/bash
          # b.nelissen

          # spinner demo code

          # stepsize is the number of characters per animation step
          # frames is the frame-art
          # stepsize=1; frames='⌜⌝⌟⌞'
          # stepsize=1; frames='🕐🕜🕑🕝🕒🕞🕓🕟🕔🕠🕕🕡🕖🕢🕗🕣🕘🕤🕙🕥🕚🕦🕛🕧'
          # stepsize=1; frames='⛔️ '
          stepsize=12; frames=' 🎾 | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 / | 🎾/ | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 | '

          # the loop
          while true; do
          s=$(( (s+$stepsize) %${#frames} ));
          printf "r${frames:$s:$stepsize}"
          sleep 0.2
          done


          Animated gif:




          tennisball







          share|improve this answer


























          • Good ideas, but since your post is not an answer in itself I think you should either elaborate until it's one, or edit a previous answer and put your code in that.

            – PetaspeedBeaver
            5 hours ago
















          0














          Some creativity while waiting.



          #!/bin/bash
          # b.nelissen

          # spinner demo code

          # stepsize is the number of characters per animation step
          # frames is the frame-art
          # stepsize=1; frames='⌜⌝⌟⌞'
          # stepsize=1; frames='🕐🕜🕑🕝🕒🕞🕓🕟🕔🕠🕕🕡🕖🕢🕗🕣🕘🕤🕙🕥🕚🕦🕛🕧'
          # stepsize=1; frames='⛔️ '
          stepsize=12; frames=' 🎾 | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 / | 🎾/ | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 | '

          # the loop
          while true; do
          s=$(( (s+$stepsize) %${#frames} ));
          printf "r${frames:$s:$stepsize}"
          sleep 0.2
          done


          Animated gif:




          tennisball







          share|improve this answer


























          • Good ideas, but since your post is not an answer in itself I think you should either elaborate until it's one, or edit a previous answer and put your code in that.

            – PetaspeedBeaver
            5 hours ago














          0












          0








          0







          Some creativity while waiting.



          #!/bin/bash
          # b.nelissen

          # spinner demo code

          # stepsize is the number of characters per animation step
          # frames is the frame-art
          # stepsize=1; frames='⌜⌝⌟⌞'
          # stepsize=1; frames='🕐🕜🕑🕝🕒🕞🕓🕟🕔🕠🕕🕡🕖🕢🕗🕣🕘🕤🕙🕥🕚🕦🕛🕧'
          # stepsize=1; frames='⛔️ '
          stepsize=12; frames=' 🎾 | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 / | 🎾/ | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 | '

          # the loop
          while true; do
          s=$(( (s+$stepsize) %${#frames} ));
          printf "r${frames:$s:$stepsize}"
          sleep 0.2
          done


          Animated gif:




          tennisball







          share|improve this answer















          Some creativity while waiting.



          #!/bin/bash
          # b.nelissen

          # spinner demo code

          # stepsize is the number of characters per animation step
          # frames is the frame-art
          # stepsize=1; frames='⌜⌝⌟⌞'
          # stepsize=1; frames='🕐🕜🕑🕝🕒🕞🕓🕟🕔🕠🕕🕡🕖🕢🕗🕣🕘🕤🕙🕥🕚🕦🕛🕧'
          # stepsize=1; frames='⛔️ '
          stepsize=12; frames=' 🎾 | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 / | 🎾/ | 🎾 | | 🎾 | | 🎾 | | 🎾 | | 🎾 | '

          # the loop
          while true; do
          s=$(( (s+$stepsize) %${#frames} ));
          printf "r${frames:$s:$stepsize}"
          sleep 0.2
          done


          Animated gif:




          tennisball








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 6 hours ago

























          answered 6 hours ago









          CousinCocaineCousinCocaine

          7203710




          7203710













          • Good ideas, but since your post is not an answer in itself I think you should either elaborate until it's one, or edit a previous answer and put your code in that.

            – PetaspeedBeaver
            5 hours ago



















          • Good ideas, but since your post is not an answer in itself I think you should either elaborate until it's one, or edit a previous answer and put your code in that.

            – PetaspeedBeaver
            5 hours ago

















          Good ideas, but since your post is not an answer in itself I think you should either elaborate until it's one, or edit a previous answer and put your code in that.

          – PetaspeedBeaver
          5 hours ago





          Good ideas, but since your post is not an answer in itself I think you should either elaborate until it's one, or edit a previous answer and put your code in that.

          – PetaspeedBeaver
          5 hours 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%2f225179%2fdisplay-spinner-while-waiting-for-some-process-to-finish%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

          Entries order in /etc/network/interfaces

          新発田市

          Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)