我怎么能脚本一个蝙蝠或cmd停止和启动一个服务可靠的错误检查(或让我知道它不成功的原因)?
当前回答
您可以使用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%
免责声明:这是我从脑子里写出来的,但我认为它会起作用。
其他回答
我们愿意认为“网络停止”将停止服务。遗憾的是,现实并不是那么非黑即白。如果停止服务的时间较长,则该命令将在服务停止之前返回。但是,除非检查errorlevel,否则您不会知道。
解决方案似乎是循环查找服务的状态,直到停止,并在每次循环中暂停。
但话说回来……
我看到第一个服务需要很长时间才能停止,然后后续服务的“净停止”似乎什么也不做。在服务管理器中查看服务,它的状态仍然是“Started”—没有更改为“stopped”。然而,我可以使用SCM手动停止第二个服务,它将在3或4秒内停止。
net start [serviceName]
and
net stop [serviceName]
清楚地告诉你他们是成功了还是失败了。例如
U:\>net stop alerter
The Alerter service is not started.
More help is available by typing NET HELPMSG 3521.
如果从批处理文件运行,则可以访问返回代码的ERRORLEVEL。0表示成功。高于这个数值意味着失败。
作为bat文件,error.bat:
@echo off
net stop alerter
if ERRORLEVEL 1 goto error
exit
:error
echo There was a problem
pause
输出如下所示:
U:\>error.bat
The Alerter service is not started.
More help is available by typing NET HELPMSG 3521.
There was a problem
Press any key to continue . . .
返回代码
- 0 = Success
- 1 = Not Supported
- 2 = Access Denied
- 3 = Dependent Services Running
- 4 = Invalid Service Control
- 5 = Service Cannot Accept Control
- 6 = Service Not Active
- 7 = Service Request Timeout
- 8 = Unknown Failure
- 9 = Path Not Found
- 10 = Service Already Running
- 11 = Service Database Locked
- 12 = Service Dependency Deleted
- 13 = Service Dependency Failure
- 14 = Service Disabled
- 15 = Service Logon Failure
- 16 = Service Marked For Deletion
- 17 = Service No Thread
- 18 = Status Circular Dependency
- 19 = Status Duplicate Name
- 20 = Status Invalid Name
- 21 = Status Invalid Parameter
- 22 = Status Invalid Service Account
- 23 = Status Service Exists
- 24 = Service Already Paused
编辑20.04.2015
返回代码:
NET命令不会返回文档中的Win32_Service类返回码(服务未激活,服务请求超时等),对于许多错误将简单地返回Errorlevel 2。
看这里:http://ss64.com/nt/net_service.html
我只是使用上面Jonas的例子,创建了一个完整的0到24个错误级别的列表。另一篇文章是正确的,网络启动和网络停止只使用errorlevel 0表示成功,2表示失败。
但这对我来说很管用:
net stop postgresql-9.1
if %errorlevel% == 2 echo Access Denied - Could not stop service
if %errorlevel% == 0 echo Service stopped successfully
echo Errorlevel: %errorlevel%
将停止改为开始并反向工作。
SC可以用服务做任何事情……启动,停止,检查,配置,和更多…
我正在用c#写一个windows服务,停止/卸载/构建/安装/启动循环太累人了。写了一个小脚本,命名为reploy.bat,并放在我的Visual Studio输出目录中(其中包含构建的可执行服务)来自动化循环。
只要设置这3个变量
servicename:显示在Windows服务控制面板(services.msc)
Slndir:包含解决方案(.sln)文件的文件夹(不是完整路径)
Binpath:从构建到服务可执行文件的完整路径(不是文件夹路径)
注意:需要从Visual Studio开发人员命令行运行msbuild命令才能工作。
SET servicename="My Amazing Service"
SET slndir="C:dir\that\contains\sln\file"
SET binpath="C:path\to\service.exe"
SET currdir=%cd%
call net stop %servicename%
call sc delete %servicename%
cd %slndir%
call msbuild
cd %bindir%
call sc create %servicename% binpath=%binpath%
call net start %servicename%
cd %currdir%
也许这能帮助到某些人:)
推荐文章
- 在批处理文件中处理子字符串的最佳方法是什么?
- 在Sublime Text 2中使用Ctrl+D进行多个选择时,我如何跳过匹配?
- #定义WIN32_LEAN_AND_MEAN具体排除了什么?
- 如何使用文件的行作为命令的参数?
- 如何添加一个设置路径只执行批处理文件?
- 最好的轻量级web服务器(只有静态内容)的Windows
- npm犯错!错误:EPERM:操作不允许,重命名
- 崇高的文本从命令行
- 如何保持Python脚本输出窗口打开?
- 使用.bat文件检查文件夹是否存在
- BAT文件:打开新的cmd窗口并在其中执行命令
- 如何做一个非贪婪匹配在grep?
- 如何查看Windows库(*.lib)的内容
- 在Windows中删除大文件夹最快的方法是什么?
- 如何在IntelliJ IDEA中输入命令行参数?