我怎么能脚本一个蝙蝠或cmd停止和启动一个服务可靠的错误检查(或让我知道它不成功的原因)?
当前回答
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
其他回答
对我来说,使用net start和net stop的返回代码似乎是最好的方法。试着看看这个:Net Start返回代码。
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
您可以使用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%
免责声明:这是我从脑子里写出来的,但我认为它会起作用。
使用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
SC可以用服务做任何事情……启动,停止,检查,配置,和更多…
推荐文章
- 如何在Mac上的命令行安装JQ ?
- 在Windows批处理脚本中格式化日期和时间
- 映射一个网络驱动器供服务使用
- 如何在windows中使用命令提示符(cmd)列出文件。我试过在Linux中使用“ls”,但它显示一个错误?
- 在命令行中使用Firefox截取完整页面的截图
- 在pip install -U中“-U”选项代表什么
- 如何将参数转发到bash脚本中的其他命令?
- Windows上最好的免费c++分析器是什么?
- 如何在Windows上运行多个Python版本
- 运行计划任务的最佳方式
- Windows上Git文件的权限
- 如何同时安装Python 2。3. Python。Windows下的x
- 使用Wget进行多个同时下载?
- Unix列表命令'ls'可以输出数值chmod权限吗?
- 如何从命令行重置Jenkins安全设置?