Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

September 25, 2001 12:00 AM

Shell Scripting 101, Lesson 10

Windows IT Pro
InstantDoc ID #22582
Rating: (2)
Downloads
22582.zip

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.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    Feb 24, 2005

    All the 10 lessons are really helpful for beginners like me. Thank you so much. Krishnamoorthi.BM.

  • forrukh
    9 years ago
    Oct 27, 2003

    I want to poweroff my PC running on Linux platform automatically after a certain period of time from logon using shell script. What will be the code?

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.