简体   繁体   English

从批处理文件中重复执行Windows命令?

[英]Repeatedly execute a Windows command from a batch file?

I'm not too familiar with batch files for Windows, so this may seem like a beginner question. 我对Windows的批处理文件不太熟悉,所以这看起来像是一个初学者的问题。 How could I "loop", or repeat, a command like the following? 我怎么能“循环”或重复一个如下命令呢?

shutdown -a

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: 我同意评论者@reuben,你可能应该寻找其他选项(包括让那个同事自己“修复”),但无论如何这里是一种重复运行命令的方法:

: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. 此外,它会对您的系统造成“轻度恐怖”,至少在csrss.exe (或conhost.exe )进程中,因为控制台窗口将忙于打印消息。 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 . 因此,您可能希望至少重定向输出( shutdown -a > NUL 2>&1 )和/或在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. 抱歉。

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到每台机器的脚本并向其发送shutdown命令:

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. 如果你的意思是在同一台计算机上循环关闭命令,你可以将shutdown命令放在〜/ .bashrc中,如果你真的想要的话。 But make sure you have a very good reason to do so. 但请确保您有充分的理由这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM