Programming Field

Timeout - DOS/Command Prompt Reference

[Windows Vista or later (*)] Performs a wait for a specified duration (timeout), equivalent to the ‘sleep’ command in Linux.

* It is available by default in Windows Vista and later versions, but in earlier Windows versions, it might be included in the Resource Kit.

Syntax

timeout[.exe] [/T] <seconds> [/NOBREAK]
[/T] <seconds>

Specifies the wait time (timeout duration) in seconds. <seconds> will be a value from -1 to 99999, and the command will wait for this duration in seconds. (‘/T’ is optional.)

If -1 is specified, it will wait indefinitely until there is some input (similar to Pause). If /NOBREAK is present, it will wait until a Ctrl+C input is received.

/NOBREAK

Disables waiting termination by normal key input. With this option, the wait will not end until the specified time elapses or Ctrl+C is entered by the user.

If /NOBREAK is not specified, the user can end the wait by entering any key, similar to Pause.

Details

Usage of Timeout

It can be used when you want to wait for a certain period, for reasons such as displaying a message. In the normal operation, it displays a message like the following, and it waits until the specified time elapses or user input is received.

Waiting for 10 seconds, press a key to continue ...

* If redirection is not performed, the seconds part is automatically counted down. If redirected, the backspace character (\x08) and the countdown number are included in the output content.

If the timeout duration is set to "-1", the seconds part will be omitted. This behavior is almost similar to waiting with the Pause command.

If /NOBREAK is specified, the message will change as follows.

Waiting for 10 seconds, press CTRL+C to quit ...

In the case of a batch file, when Ctrl+C is entered (including situations where the program being run by the batch file does not handle it as a special case), a prompt saying ‘Terminate batch job (Y/N)?’ is displayed. Here, entering Y will halt further batch processing. Therefore, the message changes from ‘Continue’ to ‘Quit’ in this context.

* It is also possible to specify both ‘/NOBREAK’ and ‘-1’ simultaneously. In this case, it can create a situation resembling a dead end. However, it's important to consider that even when using this approach, if you enter N in the prompt asking to terminate, it will proceed to the next processing.

Samples

Sample 1

timeout /T 5

Waits for 5 seconds. Even if 5 seconds have not elapsed, the wait will end if the user inputs any key.

Sample 2

timeout 5 /NOBREAK > NUL

Waits for 5 seconds without displaying a message. By using the /NOBREAK option and suppressing the output (with output redirection to NUL), you can achieve behavior similar to the Linux ‘sleep’ command.

See also