2

I'm not too familiar with batch files for Windows, so this may seem like a beginner question. How could I "loop", or repeat, a command like the following?

shutdown -a
reuben
  • 3,360
  • 23
  • 28
RandomPhobia
  • 4,698
  • 7
  • 25
  • 22
  • Do you want to repeat it a fixed number of times? Repeat it until some condition occurs? Or, perhaps it would just be more useful if you could explain the broader goal you're trying to accomplish. – reuben Jul 16 '12 at 04:45
  • @reuben - I've have someone beside me at work who likes to play around and hit the power button on my terminal. When he does I bring up the run box and type in the "shutdown -a" command but bout 50% of the time the computer is past the point of return and can't be stopped from shutting down so I thought I'd just loop it constantly. – RandomPhobia Jul 16 '12 at 05:07
  • 1
    Sorry to hear you have to deal with that! Have you looked into changing how your PC reacts to the power button getting hit? Holding down the reset button for a few seconds usually can't be canceled, but a quick push might be software interruptible... – reuben Jul 16 '12 at 05:09
  • @reuben - hmmm that's a good fix. Thanks for the suggestion – RandomPhobia Jul 16 '12 at 05:14

2 Answers2

7

I agree with commenter @reuben, that you possible should look for other options (including getting that coworker himself "fixed"), but anyway here is a way to run the command repeatedly:

:loop
shutdown -a
goto loop

Note that this might still leave a window, where it cannot cancel a shutdown because it is not "fast" enough.

Also it causes "mild terror" to your system, at least on the csrss.exe (or conhost.exe) process, because the console window will be busy printing messages. Thus you might want to redirect the output at least (shutdown -a > NUL 2>&1) and/or introduce some delay beteween the calls to shutdown. Of course the later increases the risk of not "catching" a shutdown significantly.

All in all that is not a good solution to your problem. Sorry.

Community
  • 1
  • 1
Christian.K
  • 47,778
  • 10
  • 99
  • 143
-1

Are you trying to shutdown multiple remote computers? You can write a script that ssh into each machine and send it the shutdown command:

ssh user1@remote_computer1 shutdown
ssh user2@remote_computer2 shutdown
...

If you mean you what to loop the shutdown command on the same computer, you can put the shutdown command in your ~/.bashrc, if you really want to. But make sure you have a very good reason to do so.

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
LMC
  • 302
  • 3
  • 13
  • Are you assuming that the OP has `ssh`, `bash`, and other Unix tools installed on the Windows machines in question? – reuben Jul 16 '12 at 05:02
  • 2
    @LMC - No 'Shutdown -a' at least on windows is a command to abort shutdown – RandomPhobia Jul 16 '12 at 05:08
  • @Reuben - Unless Windows has them pre-installed they probably wouldn't be on the computer and I think I would get in trouble for adding things – RandomPhobia Jul 16 '12 at 05:09
  • 2
    You are entirely correct. I don't know how I could have completed missed the word "Windows" in the question. Sorry about that. – LMC Jul 16 '12 at 14:47