我怎么能脚本一个蝙蝠或cmd停止和启动一个服务可靠的错误检查(或让我知道它不成功的原因)?
当前回答
手动重启服务是ok -服务。msc有“重新启动”按钮,但在命令行中,sc和net命令都没有“重新启动”开关,如果在cmd/bat文件中计划重新启动,服务将立即停止和启动,有时它会得到一个错误,因为服务还没有停止,它需要一段时间来关闭东西。
这可能会产生一个错误: sc停止 sc开始
插入超时是个好主意,我使用ping(它每1秒ping一次): sc停止 Ping localhost -n 60 sc开始
其他回答
这也可以代替检查代码
net start "Apache tomcat" || goto ExitError
:End
exit 0
:ExitError
echo An error has occurred while starting the tomcat services
exit 1
使用SC(服务控制)命令,它为您提供了更多的选项,而不仅仅是启动和停止。
DESCRIPTION: SC is a command line program used for communicating with the NT Service Controller and services. USAGE: sc <server> [command] [service name] ... The option <server> has the form "\\ServerName" Further help on commands can be obtained by typing: "sc [command]" Commands: query-----------Queries the status for a service, or enumerates the status for types of services. queryex---------Queries the extended status for a service, or enumerates the status for types of services. start-----------Starts a service. pause-----------Sends a PAUSE control request to a service. interrogate-----Sends an INTERROGATE control request to a service. continue--------Sends a CONTINUE control request to a service. stop------------Sends a STOP request to a service. config----------Changes the configuration of a service (persistant). description-----Changes the description of a service. failure---------Changes the actions taken by a service upon failure. qc--------------Queries the configuration information for a service. qdescription----Queries the description for a service. qfailure--------Queries the actions taken by a service upon failure. delete----------Deletes a service (from the registry). create----------Creates a service. (adds it to the registry). control---------Sends a control to a service. sdshow----------Displays a service's security descriptor. sdset-----------Sets a service's security descriptor. GetDisplayName--Gets the DisplayName for a service. GetKeyName------Gets the ServiceKeyName for a service. EnumDepend------Enumerates Service Dependencies. The following commands don't require a service name: sc <server> <command> <option> boot------------(ok | bad) Indicates whether the last boot should be saved as the last-known-good boot configuration Lock------------Locks the Service Database QueryLock-------Queries the LockStatus for the SCManager Database EXAMPLE: sc start MyService
我们愿意认为“网络停止”将停止服务。遗憾的是,现实并不是那么非黑即白。如果停止服务的时间较长,则该命令将在服务停止之前返回。但是,除非检查errorlevel,否则您不会知道。
解决方案似乎是循环查找服务的状态,直到停止,并在每次循环中暂停。
但话说回来……
我看到第一个服务需要很长时间才能停止,然后后续服务的“净停止”似乎什么也不做。在服务管理器中查看服务,它的状态仍然是“Started”—没有更改为“stopped”。然而,我可以使用SCM手动停止第二个服务,它将在3或4秒内停止。
语法总是让我....所以…
下面显式地介绍了如何向批处理文件中添加一行,如果您是两台机器上的管理员,以管理员身份运行.bat,并且两台机器位于同一个域中,则该批处理文件将终止远程服务(在另一台机器上)。机器名称遵循UNC格式\myserver
sc \\ip.ip.ip.ip stop p4_1
在这种情况下……当您在服务管理器中查看服务的属性时,p4_1是服务名称和显示名称。您必须使用服务名称。
对于你们的服务运营迷…一定要附上你的理由代码和评论!即。“4”等于“计划”和注释“停止服务器维护”
sc \\ip.ip.ip.ip stop p4_1 4 Stopping server for maintenance
您可以使用NET START命令,然后检查ERRORLEVEL环境变量,例如。
net start [your service]
if %errorlevel% == 2 echo Could not start service.
if %errorlevel% == 0 echo Service started successfully.
echo Errorlevel: %errorlevel%
免责声明:这是我从脑子里写出来的,但我认为它会起作用。
推荐文章
- 在批处理文件中处理子字符串的最佳方法是什么?
- 在Sublime Text 2中使用Ctrl+D进行多个选择时,我如何跳过匹配?
- #定义WIN32_LEAN_AND_MEAN具体排除了什么?
- 如何使用文件的行作为命令的参数?
- 如何添加一个设置路径只执行批处理文件?
- 最好的轻量级web服务器(只有静态内容)的Windows
- npm犯错!错误:EPERM:操作不允许,重命名
- 崇高的文本从命令行
- 如何保持Python脚本输出窗口打开?
- 使用.bat文件检查文件夹是否存在
- BAT文件:打开新的cmd窗口并在其中执行命令
- 如何做一个非贪婪匹配在grep?
- 如何查看Windows库(*.lib)的内容
- 在Windows中删除大文件夹最快的方法是什么?
- 如何在IntelliJ IDEA中输入命令行参数?