WAITFOR - CMD


Overview

The WAITFOR command in Windows CMD is used to synchronize the execution of batch files across different computers or sessions by waiting for a signal to be sent or for a specific timeout to occur. This tool is particularly useful in network operations and administrative tasks where processes must be coordinated between multiple systems.

Syntax

The basic syntax for WAITFOR is as follows:

WAITFOR [/S system [/U username [/P [password]]]] /SI [signal]
WAITFOR [/T timeout] /SI [signal]
  • /S system : Specifies the remote system to wait for or send the signal.
  • /U username : Specifies the user context under which the command should execute.
  • /P password : Specifies the password for the given user context. Prompts for input if omitted.
  • /T timeout : Defines the time-out period in seconds. If the signal is not received within this time, the command will exit.
  • /SI signal : Specifies that the signal is to be sent to the local or specified remote system.

Options/Flags

  • /S system: This option is used when the signal needs to be sent or listened for on a remote system. Usage in larger network environments where multiple machines need coordination.
  • /U username and /P password: Credentials for accessing the remote system, required if the session doesn’t have implicit permissions.
  • /T timeout: Useful for preventing indefinite waits, this sets the maximum time WAITFOR will wait for a signal before timing out and terminating.
  • /SI signal: Initiates a signal. This is crucial for letting other systems or sessions know that a certain event or threshold has been reached.

Examples

  1. Basic Waiting for Signal:

    WAITFOR DoneProcessing
    

    This command will wait indefinitely for the signal “DoneProcessing” to be sent from any session on the same machine.

  2. Waiting with Timeout:

    WAITFOR /T 300 DoneProcessing
    

    Waits for the “DoneProcessing” signal for up to 300 seconds before timing out.

  3. Sending a Signal:

    WAITFOR /SI DoneProcessing
    

    Sends the “DoneProcessing” signal to all waiting processes on the local machine.

  4. Remote Signal Wait:

    WAITFOR /S server01 /U admin /P password /T 600 RemoteSignal
    

    Waits 600 seconds for the “RemoteSignal” from server01 under admin credentials.

Common Issues

  • Permissions Issues: Ensure the user has appropriate permissions, especially when interacting with remote systems.
  • Network Delays: Network issues can delay or prevent signals. Always check network connectivity and configurations.
  • Timeout Settings: Improper timeout can lead either to too long a wait or too short. Be mindful when setting /T.

Integration

WAITFOR can be integrated with other commands for coordinated batch operations. For example:

WAITFOR /SI StartBackup
robocopy C:\data \\backupserver\data
WAITFOR /SI BackupComplete

This script sends a signal to start backup, performs the backup, and then signals its completion.

  • SCHTASKS: Schedule tasks on a local or a remote machine.
  • NET: Useful for various network operations including stopping and starting services.

For more details, refer to the official Microsoft documentation.