我该如何编写一个蝙蝠或cmd脚本来停止并可靠地启动服务并进行错误检查(或者让我知道它因任何原因而不成功)?停止并通过批处理或cmd文件启动服务?
回答
使用SC
(业务控制)命令,它给你的不仅仅是start
& stop
很多更多的选择。
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
使用net start
和net stop
的返回码对我来说似乎是最好的方法。试试看看这个:Net Start return codes。
链接的页面不再存在。因此,现在更多的是评论。 – Neolisk 2016-11-08 20:41:22
net start [serviceName]
和
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
编辑2015年4月20日
返回代码:
The NET command does not return the documented Win32_Service class return codes (Service Not Active,Service Request Timeout, etc) and for many errors will simply return Errorlevel 2.
这个答案是我们应该看到的更多!工作很好。 – Pokus 2008-09-25 15:22:56
不是我所有的 - 返回代码来自社区编辑... – 2008-09-25 18:49:51
好吧,这是一个很好的回应。谢谢一堆。 – 2009-02-19 20:35:51
可以使用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
可以做一切的服务...启动,停止,检查,配置,还有更多...
,或者你可以用这个CMD启动远程服务:sc \\<computer> start <service>
代替检查代码,这个工程太
net start "Apache tomcat" || goto ExitError
:End
exit 0
:ExitError
echo An error has occurred while starting the tomcat services
exit 1
语法总是让我....所以...
这里是明确如何添加一行到批处理文件,如果你是两台机器上的管理员,将杀死远程服务(在另一台机器上),以管理员身份运行.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
我们愿意认为“净停止”将停止服务。可悲的是,现实不是那么黑白。如果服务需要很长时间才能停止,那么该命令将在服务停止前返回。但是,您不会知道,除非您检查错误级别。
的解决方案似乎是只要不停止,有暂停,每次轮循环回路一轮寻找服务的状态。
但话又说回来......
我看到的第一个服务需要很长的时间停止,那么“净停止”为后续的服务只是显得无能为力。查看服务管理器中的服务,其状态仍为“已启动” - 不会更改为“正在停止”。然而,我可以使用SCM手动停止第二项服务,并在3或4秒内停止。
我已经创造了这个我个人的批处理文件,我的是稍有不同,但随意修改,你认为合适。 我刚才创建了这个,因为我很无聊,想要让人们能够输入结束,开始,停止或设置为自动的简单方法。这个BAT文件只是要求你输入服务名称,它会为你完成剩下的工作。我没有意识到他正在寻找一些说错误的东西,我一定误解了这部分。尽管通常可以通过在行尾输入>> output.txt来完成。
的VAR%%,不过是为了用户能够输入自己的服务到这一点,而不必去要开始每次修改bat文件/停止不同的服务方式。
如果我错了,任何人都可以随时纠正我在此。
@echo off
set /p c= Would you like to start a service [Y/N]?
if /I "%c%" EQU "Y" goto :1
if /I "%c%" EQU "N" goto :2
:1
set /p var= Service name:
:2
set /p c= Would you like to stop a service [Y/N]?
if /I "%c%" EQU "Y" goto :3
if /I "%c%" EQU "N" goto :4
:3
set /p var1= Service name:
:4
set /p c= Would you like to disable a service [Y/N]?
if /I "%c%" EQU "Y" goto :5
if /I "%c%" EQU "N" goto :6
:5
set /p var2= Service name:
:6
set /p c= Would you like to set a service to auto [Y/N]?
if /I "%c%" EQU "Y" goto :7
if /I "%c%" EQU "N" goto :10
:7
set /p var3= Service name:
:10
sc start %var%
sc stop %var1%
sc config %var2% start=disabled
sc config %var3% start=auto
我刚才使用了Jonas的例子,并创建了0到24个错误级别的完整列表。其他职位是正确的,net start
和net stop
只用成功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%
变化stop
到start
和反向工程。
- 1. 从windows批处理文件启动和停止node.js服务器
- 2. 停止Windows服务是通过批处理文件
- 3. 停止服务,运行批处理文件,然后启动服务
- 4. 从另一个批处理文件启动批处理文件仅打开CMD
- 5. 批处理文件总共停止服务18然后删除文件,然后重新启动服务
- 6. Windows服务来运行批处理文件,InstallUtil /我阻止服务启动?
- 7. 使用批处理文件停止服务器?
- 8. Tomcat服务每次启动时都要通过批处理文件
- 9. 如何通过批处理文件来执行存储过程或查询,并通过批处理文件
- 10. 如何通过批处理文件启动XML和XSL文件
- 11. 从服务启动一个EXE文件,并通过发送SIGBREAK来停止它
- 12. CMD批处理文件驱动器号
- 13. 批处理文件 - 关闭通过批处理文件过程
- 14. 通过Nagio重新启动/停止/启动服务
- 15. 无法使用批处理文件正确地停止并启动tomcat服务器
- 16. 启动Windows服务并启动cmd
- 17. windows服务 - >批处理文件:访问被拒绝停止服务
- 18. 批处理文件 - 除列出的服务外,正在停止服务
- 19. 创建批处理文件以停止服务 - 服务名称无效
- 20. 批处理文件:另存为批处理文件(.cmd)
- 21. ImageMagick批处理文件停止工作
- 22. 停止使用批处理文件
- 23. 批处理文件:停止进程
- 24. 使用批处理文件停止logcat
- 25. 批处理/ CMD:将文件添加到启动列表
- 26. 如何用批处理/ cmd启动PS脚本文件?
- 27. 处理与服务相关的暂停或停止的活动
- 28. 通过REST服务批处理
- 29. Windows服务自动启动和停止,异常处理问题
- 30. 停止批处理文件只处理最后一个文件?
也许有些偏离主题(因为您要求bat&cmd指令),但是:** PowerShell **为您执行此类操作提供了很多控制和反馈。 – GvS 2008-09-25 15:19:55