Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


September 25, 2001

Shell Scripting 101, Lesson 10

RSS
Subscribe to Windows IT Pro | See More Batch Files Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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.
Pause
Echo 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 5
Putting this command in the code
Echo Here is the first line of code.
Sleep 5
Echo 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 10
Echo 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.

   Previous  [1]  2  Next 


Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: 4th of July Special Edition

An often irreverent look at some of the week's other news, including a shortened work week thanks to the 4th of July, expensive Windows 7 pricing, Bing's modest monthly gains, IE 8 heading to work, Steve Jobs back at Apple, and so much more ...

Google Wave Emulates Trends of Changing World

As collaboration continues to increase, the world and how individuals view information is evolving. What does that mean for IT? ...


Related Articles Shell Scripting 101, Lesson 9

Shell Scripting 101, Lesson 8

Shell Scripting 101, Lesson 7

Shell Scripting 101, Lesson 6

Related Events Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format

Test Drive IT Solutions and Get Free Music Downloads
Solve your toughest IT problems with these free downloads and receive 5 free music downloads!


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home asp.netPRO Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement | Reprints and Licensing