Home > bash > Whiptail – Using a progress gauge – Part 2

Whiptail – Using a progress gauge – Part 2

The initial code I shared for how to use a progress bar had a major bug. The issue was that if the process takes longer than 100 seconds to run, the progress bar will be stuck at 100%. So I finally had a look at this again and here is a better script. IMHO.

If the process is still running after say 51 seconds, reset the initial count back to say 30 seconds. So we should loop continuously between 0% to 50% to 30% until the process completes and stops running. At which point we then count down to 100%.

# Keep checking if the process is running. And keep a count.
        while (true)
            proc=$(ps aux | grep -v grep | grep -e "python")
            if [[ "$proc" == "" ]] && [[ "$percentage" -eq "0" ]];
                # Nothing to do as the process was not running when we
                # started the script.
            elif [[ "$proc" == "" ]] && [[ "$percentage" -gt "0" ]];
                # The process has finished. It is no longer running.
                # So slowly count the percentage down to 100%.
                sleep 2
                echo 98
                sleep 1
                echo 99
                sleep 1
                echo 100
                sleep 1
            elif [[ "51" -eq "$percentage" ]]
                # The process is running and taking really long.
                # Instead of running up to # 100% we instead reach 50% and
                # reset the percentage count back to 30%.
                # Now that we are back at 30% we can start counting again.
                # We will keep looping here forever until the running process stops.
            sleep 1
            echo $percentage
            percentage=$(expr $percentage + 1)
} | whiptail --title "Example" --gauge "Just another example" 6 50 0


line 7 shows that the process I am checking against is python. Simply fire up a python interactive shell on a different terminal. We would need to change this to our desired process.

line 8 handles the case where we start up the script, but the process we want to watch is not running. The progress bar will display 0% and the script will exit.
Screen Shot 2016-05-02 at 13.21.10

lines 17, 23 We can change this to whatever values we desire. The sleep 2 can be increased if you want there to be longer pause before counting down to 100.

line 25 is where we loop back from 50% to 30% until we stop. You can change these values to be whatever number you would like.
Screen Shot 2016-05-02 at 13.22.18

line 13 is called when we close the python shell running on a separate terminal. The progress bar should then move from 98% to 100% then stop.
Screen Shot 2016-05-02 at 13.30.35

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.