停止和开始一个通过服务批量或cmd的文件?
-
02-07-2019 - |
题
我怎么能脚本蝙蝠或命令停止并开始服务的可靠的错误检查(或者让我知道,这不是成功于什么原因)?
解决方案
使用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 [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日
退货代码:
NET命令不返回记录的Win32_Service类返回码(Service Not Active,Service Request Timeout等),对于许多错误,只返回Errorlevel 2.
您可以使用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%
免责声明:我是从头脑中写出来的,但我认为它会起作用。
而不是检查代码,这也适用
net start "Apache tomcat" || goto ExitError
:End
exit 0
:ExitError
echo An error has occurred while starting the tomcat services
exit 1
我为此创建了我的个人批处理文件,我的有点不同,但可以根据需要随意修改。 我不久前创建了这个,因为我很无聊,想让人们能够输入结束,开始,停止或设置为自动的简单方法。此BAT文件只是请求您输入服务名称,它将为您完成剩下的工作。我没有意识到他正在寻找任何表明任何错误的东西,我一定是误读了那一部分。虽然通常可以通过输入<!> gt; <!> gt;来完成。 line.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
使用net start
和net stop
的返回代码对我来说似乎是最好的方法。试试这个: Net Start返回代码一>
语法总是让我......所以......
如果您是两台计算机上的管理员,以管理员身份运行.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
我们想认为<!>“net stop <!>”;将停止服务。可悲的是,现实并非那种黑与白。如果服务需要很长时间才能停止,则该命令将在服务停止之前返回。但是,除非你检查errorlevel,否则你不会知道。
解决方案似乎是循环查找服务状态,直到它停止,每次循环都暂停。
但是又一次......
我看到第一个服务需要很长时间才能停止,然后<!>“net stop <!>”;对于后续服务似乎什么都不做。查看服务管理器中的服务,其状态仍为<!> quot; Started <!> quot; - 没有更改为<!>“;停止<!>”;然而,我可以使用SCM手动停止第二次服务,并在3或4秒内停止。
或者您可以使用此cmd启动远程服务:sc \\<computer> start <service>
我刚刚使用了上面的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
并反向运作。
手动服务重启没问题 - services.msc有<!>“重启<!>”;按钮,但在命令行中,sc和net命令都缺少<!> quot; restart <!> quot;切换并且如果在cmd / bat文件中安排了重新启动,则服务会立即停止并启动,有时会出现错误,因为服务尚未停止,需要一些时间来关闭服务。
这可能会产生错误: sc停止 sc start
插入超时是一个好主意,我使用ping(每1秒ping一次): sc停止 ping localhost -n 60 sc start
SC
可以完成所有服务......启动,停止,检查,配置等等......
有时你会发现停止不起作用..
我的SQlServer有时会这样做。使用以下命令行可以杀死它。如果你真的需要你的脚本来杀死不停止的东西。我会这样做作为最后的手段
taskkill /pid [pid number] /f
这里是Windows10命令,开始系统恢复使用批:
sc config swprv start= Auto
你还可以像那些命令:
更改登记册的价值自动启动系统恢复
REG添加"HKLM软件\Microsoft Windows NT\CurrentVersion\SystemRestore"/v DisableSR/t数/d0/f
创建一个系统的恢复点
Wmic.exe /空间:\根\默认路径SystemRestore呼CreateRestorePoint"djibe救了你的电脑",100,12
改变系统的恢复磁盘的使用情况
vssadmin调整shadowstorage为=C:在=C:/maxsize=10%
享受
我在C#中编写一个Windows服务,停止/卸载/构建/安装/启动循环太累了。写了一个迷你脚本,称之为reploy.bat
并放入我的Visual Studio输出目录(具有内置服务可执行文件的目录)中以自动化循环。
设置这3个变量
servicename
:这显示在Windows服务控制面板(services.msc)
slndir
:包含解决方案(.sln)文件的文件夹(不是完整路径)
binpath
:构建
注意:这需要从Visual Studio Developer命令行运行,以使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%
也许这有助于某人:)