2

I have a batch script that is executed outside of an interactive command prompt. I want to sleep for 30 seconds between two commands in it?

I've read in some responses to similar questions that you can use timeout or choice, but these fail in a non-interactive session.

ERROR: Input redirection is not supported, exiting the process immediately.

I've read in other responses to similar questions that you can use waitfor, but this will set an error level and I rely on those in the overall script.

I've seen using a ping that will fail ping -n 1 -w 30000 192.168.not.reachable > NUL and this appears to work, but that just seems wrong and I can't rely on an single IP being not reachable on all systems I'll deploy to.

Is there another way for me to try?

Thanks.

EDITS:

This answer (How to sleep in a batch file?) definitely has related material, but doesn't address the need for a solution that works outside an interactive shell. It does mention the only alternative I have now: ping, but as I ask above, I'm looking for alternatives.

Also, thanks for the PowerShell suggestions, but that's not possible now.

Jobu
  • 121
  • 1
  • 4
  • You got the ping method wrong. You would use a host that you *can* reach (localhost) and ping it 30 times, resulting in a 30sec wait. `ping -n 30 127.0.0.1`. It's still silly, using PS would be the more modern alternative. – Sven Aug 30 '16 at 00:37
  • 3
    Possible duplicate of [How to sleep in a batch file?](http://serverfault.com/questions/432322/how-to-sleep-in-a-batch-file) – Jeter-work Aug 30 '16 at 00:37
  • @Xalorous: This answer doesn't address the "non-interactive" restriction, and pinging `192.0.2.0/24` is still silly. – Sven Aug 30 '16 at 00:43
  • It does answer it. – Jeter-work Aug 30 '16 at 00:45
  • @Xalorous: happy to remove a dupe, but I agree w/ Sven . . . they answer you cite doesn't cover options for non-interactive sessions. If you can quote from there, I'd appreciate it. – Jobu Aug 30 '16 at 04:04
  • @Sven: PowerShell is not an option at this point. Thanks for the `-n 30` improvement. At least then the silly hack is portable to all deployments. I gather there's nothing "not silly" available for this? – Jobu Aug 30 '16 at 04:06
  • The timeout would get interrupted if the user hits any key; however, the command also accepts the optional switch /nobreak, which effectively ignores anything the user may press, except an explicit CTRL-C: `timeout /t 30 /nobreak` Additionally, if you don't want the command to print its countdown on the screen, you can redirect its output to NUL: `timeout /t 30 /nobreak > NUL` – Jeter-work Aug 30 '16 at 17:14
  • http://www.robvanderwoude.com/wait.php There's a number of options here. Find or write a utility for the sleep function. A compiled program or a vbs or kixtart script may be your best options in this case. – Jeter-work Aug 30 '16 at 17:20
  • @Xalorous I would accept that last answer as the best. – Jobu Aug 31 '16 at 19:47

1 Answers1

0

From http://www.robvanderwoude.com/wait.php

Non-DOS Scripting

Use the SysSleep function whenever you need a time delay in Rexx scripts. SysSleep is available in OS/2's (native) RexxUtil module and in Patrick McPhee's RegUtil module for 32-bits Windows.

Use the Sleep command for time delays in KiXtart scripts.

Use WScript.Sleep, followed by the delay in milliseconds in VBScript and JScript (unfortunately, this method is not available in HTAs).

The following batch code uses a temporary VBScript file to generate an accurate delay:

@ECHO OFF
REM %1 is the number of seconds for the delay, as specified on the command line
> "%Temp%.\sleep.vbs" ECHO WScript.Sleep %~1 * 1000
>> CSCRIPT //NoLogo "%Temp%.\sleep.vbs"
>> DEL "%Temp%.\sleep.vbs"

Not my solution, I just found it:

Jeter-work
  • 825
  • 4
  • 15