简体   繁体   English

创建批处理脚本以在多次重启或停止服务后关闭计算机

[英]Creating a batch script to shut down machine after multiple restart or stopping of services

I have created a batch script that is able to shut down the machine after multiple restarts or halts of a particular window service. 我创建了一个batch脚本,该脚本可以在多次重新启动或中止特定窗口服务后关闭计算机。

However, I have no idea about how to make the script work automatically instead of running it manually . 但是,我不知道如何使脚本自动运行而不是手动运行 I have tried testing the script file by setting it up under services.msc and setting it to run a program (batch file) after subsequent failures under this particular window service but when I stopped or restart the service 3 times manually, the batch script did not run at all . 我尝试通过在services.msc下设置脚本文件并将其设置为在此特定窗口服务下随后失败后run a program (batch file)来测试脚本文件,但是当我手动停止或重新启动该服务3次时,批处理脚本确实根本不运行。

So I'm thinking it could be that services.msc only recognizes failures before running the script instead of stopping or restarting the service. 因此,我认为这可能是services.msc仅在运行脚本之前识别故障,而不是停止或重新启动服务。 If so, is there any other way to run the script? 如果是这样,还有其他方法可以运行脚本吗?

This is my batch script: 这是我的批处理脚本:

sc query "AVG Antivirus" | find "STOPPED" 
    if "%ERRORLEVEL%"=="3" 
    (
        shutdown.exe /s /t 00
    )

%errorlevel% doesn't "count" - it always gives 0 for "success" and NOT 0 (usually 1 , but depends on the command). %errorlevel%不会“计数”-它总是将0表示“成功”而NOT 0 (通常为1 ,但取决于命令)。 So you have to implement an own counter: 因此,您必须实现自己的计数器:

set count=0
:loop
  sc query "AVG Antivirus" | find "STOPPED" && set /a count+=1
  if %count% geq 3 goto :continue
  timeout /t 5
goto :loop
:continue
shutdown.exe /s /t 00

|| works as "if previous command ( find here) failed then" (the opposite is && "if previous command was successful") 用作“如果先前的命令( find此处find )失败了”(与&&相反,如果先前的命令成功了)

I'm not sure if you need the service to be RUNNING or STOPPED. 我不确定您是否需要该服务正在运行或已停止。 Difference is just using && or || 区别只是使用&&|| )

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

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