当前位置:  开发笔记 > 编程语言 > 正文

通过批处理或cmd文件停止并启动服务?

如何解决《通过批处理或cmd文件停止并启动服务?》经验,为你挑选了7个好方法。

我如何编写一个bat或cmd脚本来通过错误检查可靠地停止和启动服务(或者让我知道它无论出于何种原因都不成功)?



1> Ferruccio..:

使用SC(服务控制)命令,它为您提供了比start&更多的选项stop.

  DESCRIPTION:
          SC is a command line program used for communicating with the
          NT Service Controller and services.
  USAGE:
      sc  [command] [service name]  ...

      The option  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   


SC的问题是命令立即返回,而不是在操作完成后返回.如果要通过批处理文件重新启动服务(停止然后启动),则停止立即返回,然后启动失败,因为服务未停止.操作完成后,网络停止/开始返回,因此没有此问题.
那个屏幕转储刚赢了奖!谢谢,我甚至都不知道'sc'!

2> Bill Michell..:
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

编辑20.04.2015

退货代码:

NET命令不返回记录的Win32_Service类返回码(服务未激活,服务请求超时等),并且对于许多错误,将仅返回Errorlevel 2.

请看这里:http://ss64.com/nt/net_service.html


这个答案是我们应该看到的更多!干得好.
这个答案更好,因为“ net”命令会阻塞并等待服务启动或停止,然后再继续执行批处理脚本。
嗯,这是一个很好的回应.谢谢一堆.

3> Jonas Gulle..:

您可以使用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%

免责声明:我从头脑中写下了这个,但我认为它会起作用.



4> vanval..:

这不是检查代码,而是也可以

net start "Apache tomcat" || goto ExitError

:End  
exit 0  

:ExitError  
echo An error has occurred while starting the tomcat services  
exit 1  



5> Nathanial Wi..:

我为此创建了我的个人批处理文件,我的有点不同,但可以根据需要随意修改.我不久前创建了这个,因为我很无聊,想让人们能够输入结束,开始,停止或设置为自动的简单方法.此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


我前一阵子创建它的原因是我很无聊,并且想为人们提供一种简单的方法来输入结束,开始,停止或设置为自动。该BAT文件仅要求您输入服务名称,其余的将由您完成。

6> ZombieSheep..:

从使用返回代码net start,并net stop似乎对我最好的方法.看看这个:Net Start返回代码.



7> ATSiem..:

语法总是让我....所以......

如果您是两台计算机上的管理员,以管理员身份运行.bat,并且计算机位于同一域中,则明确说明如何向批处理文件添加一行以终止远程服务(在另一台计算机上).机器名称遵循UNC格式\ myserver

sc \\ip.ip.ip.ip stop p4_1

在这种情况下...当您在Service Manager中查看服务的属性时,p4_1既是服务名称又是显示名称.您必须使用服务名称.

对于您的服务行动爱好者...请务必附上您的原因代码和评论!即'4'等于'计划'并评论'停止维护服务器'

sc \\ip.ip.ip.ip stop p4_1 4 Stopping server for maintenance

推荐阅读
低调pasta_730
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有