When you run a command that can take a long time, it’s usually a good idea to keep the user informed that something is, in fact, happening. In a GUI, this notification could be a progress indicator shown as a percentage, or a wild estimate of how long it might take to complete the command. It’s always nice to give some feedback, even if it’s completely wrong.

On the AIX command line, however, the message “something is happening” doesn’t always have that level of sophistication. Still, there’s an interesting approach that's used when running the mksysb command; the command displays a dot every 10 seconds until the command completes successfully.

There's also a percentage estimate of the number of files that have been backed up. Here’s how the output looks:

# mksysb -ie  /backup/myhost.mksysb

Creating information file (/image.data) for rootvg..

Creating list of files to back up.
..
Backing up 71494 files..............................
37745 of 71494 files (52%)....................

Note that the number given is a percentage of the number of files backed up. Some files may take more time to back up than others, so it’s not a reliable indicator of how long the backup will take to complete.

The mksysb command is actually a script, which is contained in the directory /usr/bin. You should never change the contents of this command, as you could break your operating system backup, which isn't a good career move. Still, that doesn’t need to stop us from being curious how the command displays those dots every 10 seconds.

Here’s the relevant code from the script.

# As a progress indicator print dots every 10 seconds during
# the system backup
while :; do
        ${sleep} 10
        echo ".\c"
done &

Let’s start from the top with the line:

while :; do

This phrase means “keep doing the following forever” (or until the script completes or is killed). This loop is closed off with the “done” on the last line. The ampersand (&) means it should run in background. In other words, this function runs without waiting around for the loop to complete. The loop gets started and the script continues with the next step in the mksysb backup.

The "sleep 10" part of the code simply means “wait for 10 seconds”. But why is the sleep command set to the variable ${sleep}? Actually, within the mksysb script, a number of critical commands are set as variables to ensure they use the correct path. So, in case you have overwritten the PATH variable or created a script or alias called sleep (please don’t do that!), the mksysb command will know to use the command specified in this line:

sleep=/usr/bin/sleep

If you run the variable ${sleep} (the curly braces are just for clarity), it will actually run the command /usr/bin/sleep and the 10 indicates how long to wait in seconds.

The next line is the one that actually displays the dot:

echo ā€œ.\cā€

The dot is obvious enough, but what’s that \c for? This addition means “don’t put a carriage return after displaying the dot.” Ordinarily, when you run the echo command, the command would start a new line after it has displayed its message.

You can check out the mksysb script if you’re curious about how and when it counts the number of files in the backup. If all goes well, you should see a message like this one:

71494 of 71494 files (100%)
0512-038 mksysb: Backup Completed Successfully.

You can use this loop in your own scripts if you have a job that runs for a while and you want to have some feedback to let you know that something's happening.

As you know, the mksysb backup is critical for recovering the AIX operating system, so it’s typically run as a scheduled job with its output entered in a log file. However, if you do run this script interactively, it’s helpful to get a command line equivalent of a progress bar.