Shell Scripting 101, Lesson 10
Timing is everything—even in scripts. This last lesson in the “Shell Scripting 101” series looks at how you can control the timing in your scripts.
September 24, 2001
Timing is everything—even in scripts. You can control the timing in your scripts with such commands as Pause, Sleep, Timeout, Waitfor, and Start. Some of these commands are part of the Windows scripting language; others are from utilities in the Microsoft Windows NT Server 4.0 Resource Kit.
The Pause Command
You can use the Pause command to stop the execution of scripts. This command displays the message Press any key to continue and stops the script’s execution until the user presses a key. For example, if you run the code
Echo Here is the first line of code.PauseEcho Here is the second line of code.
you receive the results that Figure 1 shows.
Scriptwriters often use the Pause command to stop a script’s execution so that they can read command output before it scrolls off the screen. However, if a script contains the Pause command, you can’t use Task Scheduler to run that script. (Task Scheduler comes with Microsoft Internet Explorer—IE—4.x and later.) The script will never finish executing because no one is present to press a key to resume the script’s execution. You’ll likely want to use Task Scheduler because it’s easier to use and provides more functionality than the At command and the resource kit’s Winat utility.
The Sleep and Timeout Commands
Although you can’t use the Pause command with Task Scheduler, you can use the resource kit’s Sleep and Timeout utilities. Both utilities let you pause a script for a specified number of seconds. For example, if you want to use the Sleep utility to pause a script for 5 seconds, you can use the command
Sleep 5Putting this command in the codeEcho Here is the first line of code.Sleep 5Echo Here is the second line of code.
prompts the command processor to display the message Here is the first line of code, pause for 5 seconds, then automatically continue to the next line and display the message Here is the second line of code.
Similarly, you can use the Timeout command to pause a script a specified number of seconds. However, the Timeout command differs from the Sleep command in one important respect: Two events can prompt the script to resume running. You can opt to have the script automatically continue executing after the specified number of seconds has elapsed, or you can manually prompt the script to resume before that time is up by pressing a key on the keyboard.
For example, the code
Echo Here is the first line of code.Timeout 10Echo Here is the second line of code.
first prompts the command processor to display the message Here is the first line of code. The command processor then pauses until someone presses a key or 10 seconds elapse (whichever comes first), after which it executes the next line of code. Because of this feature, the Timeout utility is ideal for scripts that you plan to execute both manually and as scheduled tasks. The Sleep command is better when you simply need a short delay between two commands or two sections in a script that you plan to run only with Task Scheduler.
The Waitfor Command
The resource kit’s Waitfor utility is interesting because to use it, you need two machines (typically, a server and a client) in the same domain. The best way to describe how this utility works is with an example. Begin by running the code that Listing 1 shows on the client. On the client’s screen, notice that only the first line of code runs and displays the message Here is the first line of code. Then, on the server, run the command
Waitfor –s freds-signal
This command sends a signal that triggers the client into completing the tasks you programmed in with the code in Listing 1. The –s switch specifies the signal (in this case, freds-signal) that the client waits for. When the client receives this signal, it runs the rest of the lines in Listing 1, one line at a time, until it receives another Waitfor signal. This example involves only one computer receiving the signal. Multiple machines can wait for the same signal.
The Waitfor command has a –t switch that lets the client code time-out if the client doesn’t receive the specified signal. If you don’t use the –t switch, the default wait time is forever. As Listing 2 shows, you simply add to the client code the –t switch followed by the number of seconds you want the client to wait. On the server, you run the same Waitfor command you ran previously.
The Start Command
If you have a task in which you need to start several scripts either sequentially or at the same time, you can use the Start command. The Start command launches a separate command shell window to run a specified command or script.
For example, suppose you need to simultaneously launch the four scripts that Listing 3,4,5 and 6 show. Listing 7 contains a master launching script that you can use. By using the Start command, you can launch all four scripts virtually simultaneously.
Listing 8 contains a variation of the launching script. In this script, I added the /wait switch to the Start command. This switch prompts the Start command to wait for each script to finish running before launching the next command shell window.
Hooked on Scripting
This lesson completes my 10-lesson introduction to Windows shell scripting. I hope you’ve enjoyed the lessons and become hooked on scripting.
Homework Assignment
Find out what the scripts in Listings 3 through 6 are doing. Run the command
Color /?
in a command shell window.
The Start command offers many parameters and switches. Explore your options by running the command
Start /?
in a command shell window.
About the Author
You May Also Like