Home > bash > Whiptail – Using a progress gauge

Whiptail – Using a progress gauge

Problem:
A problem I faced with whiptail was how to use the progress gauge. Most examples I had seen were not very helpful for what I had in mind. My issue was that I needed the progress gauge to show the script user how far a particular activity had gone. In my case the activity was dumping a mysql database.

Solution:
1. Here is simple example of a progress gauge.

#!/bin/bash
PCT=0
( 
while test $PCT != 100; 
do 
PCT=`expr $PCT + 10`; 
echo $PCT; 
sleep 1; 
done; ) | whiptail --title "GAUGE" --gauge "Hi, this is a gauge widget" 20 70 0

2. Here is the way I finally managed to get this done.

#!/bin/bash

# Start mysqldump and send it to the background.
mysqldump --user="root" --password="password" database > database.sql & 
# Keep checking if the process is running. And keep a count.
{
        i="0"
        while (true)
        do
            proc=$(ps aux | grep -v grep | grep -e "mysqldump")
            if [[ "$proc" == "" ]]; then break; fi
            # Sleep for a longer period if the database is really big 
            # as dumping will take longer.
            sleep 1
            echo $i
            i=$(expr $i + 1)
        done
        # If it is done then display 100%
        echo 100
        # Give it some time to display the progress to the user.
        sleep 2
} | whiptail --title "Backup" --gauge "Dumping database" 8 78 0

Update:
Improved progress gauge code.

Source:
http://www.dawoodfall.net/files/scripts/bash/wget-dialog-example
http://www.fifi.org/doc/dialog/examples/
https://www.linuxquestions.org/questions/programming-9/dialog-gauge-how-to-display-progress-of-compile-broken-pipe-problem-200614/

Advertisements
Categories: bash Tags: ,
  1. December 3, 2013 at 1:26 pm

    Your mysql example made me sick. Seriously.
    Is it that you do not realize what purpose serve this kind of progress gauge display, or are you just plain evil ? I know some men just want to watch the world burn, but you’ve taken this way too far.

    (For clarification, in case you don’t want to bother reading it’s source : our fellow Rodnee here starts its mysql dump, then enters a loop where he adds one percent to the gauge display every second, and checks if the mysql dump is still running. If it isn’t, he leaves the loop and jump to 100%. I let you figure out what’s wrong with this approach, I don’t even know where to start)

    • December 4, 2013 at 6:06 am

      I am learning this stuff, so please feel free to correct me and explain how I should have done this. Thanks.

  2. Rajeev Prakash
    December 15, 2014 at 5:58 pm

    If the mysqldump takes more than 100 seconds and the gugae will display 100% and the process will be still continuing .. right ???

    • December 22, 2014 at 5:57 pm

      The idea at that time was to have the gauge “grow” depending on whether there was a process still running in the background. It would only display 100% once the proceess stops running. So to answer your question, no.

  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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: