停止并通过批处理或CMD文件启动一个服务?(Stop and Start a service via

2019-06-21 04:18发布

我如何脚本蝙蝠或cmd以停止和错误检查可靠启动服务(或让我知道,这是不是成功的无论何种原因)?

Answer 1:

使用SC (服务控制)命令,它给你更多的选择不仅仅是startstop

  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


Answer 2:

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

编辑二零一五年四月二十日

返回代码:

NET命令不返回记录Win32_Service类返回代码(服务不活跃,服务请求超时,等)和许多错误将简单地返回ERRORLEVEL 2。

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



Answer 3:

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

免责声明:我已经从我的头顶写了这个,但我认为它会工作。



Answer 4:

而不是检查代码,这个工程太

net start "Apache tomcat" || goto ExitError

:End  
exit 0  

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


Answer 5:

我创建了我个人的批处理文件对于这一点,我的是稍有不同,但随意修改,你认为合适。 我创造了这个前一阵,因为我很无聊,想作一个简单的方法让人们能够输入结束,启动,停止,或设置为自动。 此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


Answer 6:

从使用返回代码net startnet stop好像对我来说是最好的方法。 尝试看看这个: 网络开始返回代码 。



Answer 7:

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

这里是明确如何添加一行到一个批处理文件,将终止远程服务(另一台计算机上),如果你在两台机器上的管理,运行.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


Answer 8:

我们愿意认为“净停止”将停止服务。 可悲的是,现实情况是不是黑色和白色。 如果服务需要较长时间停止,停止该服务之前,该命令将返回。 你不会知道,不过,除非你检查错误级别。

该解决方案似乎是循环轮,直到它停止,以暂停每轮循环寻找服务的状态。

但话又说回来...

我看到的第一个服务需要很长的时间停止,那么“净停止”为后续的服务只是显得无能为力。 看看在服务管理器中的服务,它的状态仍然是“开始” - 没有改变“停止”。 然而,我可以停止使用SCM手动该第二服务,并且它在3或4秒后停止。



Answer 9:

或者也可以与此CMD启动远程服务: sc \\<computer> start <service>



Answer 10:

我只是用乔纳斯上面的例子,创造了0到24 errorlevels完整列表。 其他职位是正确的, net startnet 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%

更改stopstart和反向工程。



Answer 11:

手动重新启动服务就可以了 - SERVICES.MSC有“重启”按钮,但在命令行SC和net命令缺少“重新启动”开关,如果重新启动在cmd中/ bat文件安排,服务被停止,并立即开始,有时候得到一个错误,因为服务还没有停止,它需要一些时间来关闭下来。

这可能会产生一个错误:SC停止SC开始

这是一个好主意,插入超时,我用ping(它坪每隔1秒):SC停止ping本地主机-n 60 SC开始



Answer 12:

SC可以做一切的服务...启动,停止,检查,配置,还有更多...



Answer 13:

有时你会发现停止不工作..

我的SQLSERVER有时做到这一点。 使用下面的命令行杀死它。 如果你真的真的需要你的脚本杀死的东西,不会停止。 我将有它做这个作为最后的手段

taskkill /pid [pid number] /f


Answer 14:

这里是Windows 10的命令来启动使用批处理系统还原:

sc config swprv start= Auto

您可能还喜欢那些命令:

  • 更改注册表值来自动启动系统还原

    REG ADD “HKLM \ SOFTWARE \微软\的Windows NT \ CURRENTVERSION \系统还原”/ V DisableSR /吨REG_DWORD / d 0 / F

  • 创建系统还原点

    了Wmic.exe /命名空间:\ ROOT \默认路径系统还原通话CreateRestorePoint “djibe保存在您的PC”,100,12

  • 更改系统还原的磁盘使用情况

    VSSADMIN调整shadowstorage /对= C:/ ON = C:/ MAXSIZE = 10%

请享用



Answer 15:

我写在C#中的窗口服务,停止/卸载/建设/安装/启动循环变得过于劳累。 写了一个小脚本,把它称为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%

也许这可以帮助别人:)



文章来源: Stop and Start a service via batch or cmd file?