如何通过批处理文件检查服务是否正在运行并启动它,如果它没有运行?
我想编写一个执行以下操作的批处理文件:
- 检查服务是否正在运行
- 如果正在运行,请退出批处理
- 如果未运行,则启动该服务
到目前为止我用谷歌搜索的代码示例结果证明不起作用,所以我决定不发布它们。
启动服务是通过以下方式完成的:
net start "SERVICENAME"
- 如何检查服务是否正在运行,以及如何在批处理文件中创建 if 语句?
- 我有点困惑。我必须向网络启动传递什么论据?服务名称还是其显示名称?
I want to write a batch file that performs the following operations:
- Check if a service is running
- If is it running, quit the batch
- If it is not running, start the service
The code samples I googled so far turned out not to be working, so I decided not to post them.
Starting a service is done by:
net start "SERVICENAME"
- How can I check if a service is running, and how to make an if statement in a batchfile?
- I'm a bit confused. What is the argument I have to pass onto the net start? The service name or its display name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
要检查服务的状态,请使用
sc query
。对于批处理文件中的 if 块,查看文档。以下代码将检查服务
MyServiceName
的状态,如果未运行则启动它(如果服务未运行则执行 if 块):作用说明:
至于您的第二个问题,您想要传递给 net start 的参数是服务名称,而不是显示名称。
To check a service's state, use
sc query <SERVICE_NAME>
. For if blocks in batch files, check the documentation.The following code will check the status of the service
MyServiceName
and start it if it is not running (the if block will be executed if the service is not running):Explanation of what it does:
As for your second question, the argument you will want to pass to
net start
is the service name, not the display name.要切换服务,请使用以下命令;
To toggle a service use the following;
您可以使用以下命令查看服务是否正在运行:
当我为 NOD32 Antivirus 运行该服务时,我得到:
如果它被停止,我会得到:
您可以在变量中使用它来确定是否使用NET 启动与否。
服务名称应该是服务名称,而不是显示名称。
You can use the following command to see if a service is running or not:
When I run it for my NOD32 Antivirus, I get:
If it was stopped, I would get:
You can use this in a variable to then determine whether you use NET START or not.
The service name should be the service name, not the display name.
应该这样做:
That should do it:
独立于语言的版本。
Language independent version.
我刚刚找到这个线程,并且想添加到讨论中,如果该人不想使用批处理文件来重新启动服务。在 Windows 中,如果您转到“服务”、“服务属性”,然后转到“恢复”,则会有一个选项。您可以在此处设置服务的参数。喜欢如果服务停止则重新启动服务。此外,您甚至可以让第二次失败尝试执行与重新启动计算机不同的操作。
I just found this thread and wanted to add to the discussion if the person doesn't want to use a batch file to restart services. In Windows there is an option if you go to Services, service properties, then recovery. Here you can set parameters for the service. Like to restart the service if the service stops. Also, you can even have a second fail attempt do something different as in restart the computer.
如果您使用西班牙语的 Windows,请使用西班牙语(使用西班牙语的 Windows 时,代码为):
Reemplazar MYSERVICE con el nombre del servicio que se desea procesar。服务名称为服务属性。 (将 MYSERVICE 替换为要处理的服务的名称。您可以在服务属性中看到服务的名称。)
Cuando se use Windows en Español, el código debe quedar asi (when using Windows in Spanish, code is):
Reemplazar MYSERVICE con el nombre del servicio que se desea procesar. Puedes ver el nombre del servicio viendo las propiedades del servicio. (Replace MYSERVICE with the name of the service to be processed. You can see the name of the service on service properties.)
对于 Windows Server 2012,下面的内容对我有用。仅将“SERVICENAME”替换为实际服务名称:
For Windows server 2012 below is what worked for me. Replace only "SERVICENAME" with actual service name:
如果服务以这种方式启动,我还希望发送一封电子邮件,因此在 @Ic 代码中添加了一些内容,只是想我会发布它,以防它对任何人有帮助。我使用了 SendMail,但还有其他命令行选项 如何从 Windows 批处理文件发送简单的电子邮件?
I also wanted an email sent if the service was started this way so added a bit to @Ic code just thought I would post it in case it helped anyone. I used SendMail but there are other command line options How to send a simple email from a Windows batch file?
使用 Powershell 脚本启动服务。您可以将其链接到任务计划程序并定期或根据需要触发它。
将其创建为 PS1 文件,即扩展名为 PS1 的文件,然后让该文件从任务计划程序触发。
要在任务计划程序中启动停止服务
(如果您在服务器上使用它),请在参数中使用
-noprofile -executionpolicybypass -file "C:\Service Restart Scripts\StopService.PS1"
通过在 cmd 上运行相同的命令来验证它是否有效也在任务计划程序上
Starting Service using Powershell script. You can link this to task scheduler and trigger it at intervals or as needed.
Create this as a PS1 file i.e. file with extension PS1 and then let this file be triggered from task scheduler.
To start stop service
in task scheduler if you are using it on server use this in arguments
-noprofile -executionpolicy bypass -file "C:\Service Restart Scripts\StopService.PS1"
verify by running the same on cmd if it works it should work on task scheduler also
与 @DanielSerrano 的答案相关,我最近对 sc.exe 命令的本地化(即西班牙语)感到有点困惑。我的建议是查明保存数字服务状态的行和令牌并对其进行解释,这应该更加可靠:
测试环境:
Related with the answer by @DanielSerrano, I've been recently bit by localization of the
sc.exe
command, namely in Spanish. My proposal is to pin-point the line and token which holds numerical service state and interpret it, which should be much more robust:Tested with:
也许有更简单的方法?只需添加到此处的答案列表:
Maybe a much simpler way? Just adding to the list of answers here:
如果您使用
nssm
,请使用以下命令< strong>如果服务正在运行,则输出
如果服务未运行,则输出
If you are using
nssm
then use the following commandOutput if service is running
Output if service is not running