答案 0 :(得分:145)
答案 1 :(得分:29)
要切换服务,请使用以下内容;
NET START“分布式事务 协调员“|| NET STOP”分发 交易协调员“
答案 2 :(得分:19)
答案 3 :(得分:14)
答案 4 :(得分:10)
语言独立版本。
@Echo Off
Set ServiceName=Jenkins
SC queryex "%ServiceName%"|Find "STATE"|Find /v "RUNNING">Nul&&(
echo %ServiceName% not running
echo Start %ServiceName%
Net start "%ServiceName%">nul||(
Echo "%ServiceName%" wont start
exit /b 1
)
echo "%ServiceName%" started
exit /b 0
)||(
echo "%ServiceName%" working
exit /b 0
)
答案 5 :(得分:5)
我刚刚找到了这个帖子,并希望添加到讨论中,如果该人不想使用批处理文件来重新启动服务。在Windows中,如果您转到服务,服务属性,然后恢复,则有一个选项。您可以在此处设置服务的参数。如果服务停止,则重新启动服务。此外,您甚至可以尝试第二次尝试执行与重新启动计算机不同的操作。
答案 6 :(得分:4)
Cuando se使用WindowsenEspañol,elcódigideequedar asi(当使用西班牙语的Windows时,代码是):
for /F "tokens=3 delims=: " %%H in ('sc query MYSERVICE ^| findstr " ESTADO"') do (
if /I "%%H" NEQ "RUNNING" (
REM Put your code you want to execute here
REM For example, the following line
net start MYSERVICE
)
)
Reemplazar MYSERVICE con el nombre del servicio que se desea procesar。 Puedes ver el nombre del servicio viendo las propiedades del servicio。 (将MYSERVICE替换为要处理的服务的名称。您可以在服务属性上看到服务的名称。)
答案 7 :(得分:2)
@echo off
color 1F
@sc query >%COMPUTERNAME%_START.TXT
find /I "AcPrfMgrSvc" %COMPUTERNAME%_START.TXT >nul
IF ERRORLEVEL 0 EXIT
IF ERRORLEVEL 1 NET START "AcPrfMgrSvc"
答案 8 :(得分:1)
如果服务是以这种方式启动的,我还想要发送一封电子邮件,所以在@Ic代码中添加了一些内容,以为我会发布它,以防它帮助任何人。我使用了SendMail,但还有其他命令行选项How to send a simple email from a Windows batch file?
set service=MyServiceName
for /F "tokens=3 delims=: " %%H in ('sc query %service% ^| findstr " STATE"') do (
if /I "%%H" NEQ "RUNNING" (
net start %service%
for /F "tokens=3 delims=: " %%H in ('sc query %service% ^| findstr " STATE"') do (
if /I "%%H" EQ "RUNNING" (
SendMail /smtpserver localhost /to me@mydomain.com /from watchdog@mydomain.com /subject Service Autostart Notification /body Autostart on service %service% succeded.
) else (
SendMail /smtpserver localhost /to me@mydomain.com /from watchdog@mydomain.com /subject Service Autostart Notification /body Autostart on service %service% failed.
)
)
)
)
答案 9 :(得分:1)
使用Powershell脚本启动服务。您可以将其链接到任务计划程序并按时间间隔或根据需要触发它。 将其创建为PS1文件,即扩展名为PS1的文件,然后从任务调度程序中触发此文件。
开始停止服务
在任务计划程序中,如果您在服务器上使用它,请在参数
中使用它-noprofile -executionpolicy bypass -file“C:\ Service Restart Scripts \ StopService.PS1”
通过在cmd上运行相同的验证,如果它工作,它也应该在任务调度程序上工作
$Password = "Enter_Your_Password"
$UserAccount = "Enter_Your_AccountInfor"
$MachineName = "Enter_Your_Machine_Name"
$ServiceList = @("test.SocketService","test.WcfServices","testDesktopService","testService")
$PasswordSecure = $Password | ConvertTo-SecureString -AsPlainText -Force
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $UserAccount, $PasswordSecure
$LogStartTime = Get-Date -Format "MM-dd-yyyy hh:mm:ss tt"
$FileDateTimeStamp = Get-Date -Format "MM-dd-yyyy_hh"
$LogFileName = "C:\Users\krakhil\Desktop\Powershell\Logs\StartService_$FileDateTimeStamp.txt"
#code to start the service
"`n####################################################################" > $LogFileName
"####################################################################" >> $LogFileName
"###################### STARTING SERVICE ##########################" >> $LogFileName
for($i=0;$i -le 3; $i++)
{
"`n`n" >> $LogFileName
$ServiceName = $ServiceList[$i]
"$LogStartTime => Service Name: $ServiceName" >> $LogFileName
Write-Output "`n####################################"
Write-Output "Starting Service - " $ServiceList[$i]
"$LogStartTime => Starting Service: $ServiceName" >> $LogFileName
Start-Service $ServiceList[$i]
$ServiceState = Get-Service | Where-Object {$_.Name -eq $ServiceList[$i]}
if($ServiceState.Status -eq "Running")
{
"$LogStartTime => Started Service Successfully: $ServiceName" >> $LogFileName
Write-Host "`n Service " $ServiceList[$i] " Started Successfully"
}
else
{
"$LogStartTime => Unable to Stop Service: $ServiceName" >> $LogFileName
Write-Output "`n Service didn't Start. Current State is - "
Write-Host $ServiceState.Status
}
}
#code to stop the service
"`n####################################################################" > $LogFileName
"####################################################################" >> $LogFileName
"###################### STOPPING SERVICE ##########################" >> $LogFileName
for($i=0;$i -le 3; $i++)
{
"`n`n" >> $LogFileName
$ServiceName = $ServiceList[$i]
"$LogStartTime => Service Name: $ServiceName" >> $LogFileName
Write-Output "`n####################################"
Write-Output "Stopping Service - " $ServiceList[$i]
"$LogStartTime => Stopping Service: $ServiceName" >> $LogFileName
Stop-Service $ServiceList[$i]
$ServiceState = Get-Service | Where-Object {$_.Name -eq $ServiceList[$i]}
if($ServiceState.Status -eq "Stopped")
{
"$LogStartTime => Stopped Service Successfully: $ServiceName" >> $LogFileName
Write-Host "`n Service " $ServiceList[$i] " Stopped Successfully"
}
else
{
"$LogStartTime => Unable to Stop Service: $ServiceName" >> $LogFileName
Write-Output "`nService didn't Stop. Current State is - "
Write-Host $ServiceState.Status
}
}
答案 10 :(得分:0)
@Echo off
Set ServiceName=wampapache64
SC queryex "%ServiceName%"|Find "STATE"|Find /v "RUNNING">Nul&&(
echo %ServiceName% not running
echo
Net start "%ServiceName%"
SC queryex "%ServiceName%"|Find "STATE"|Find /v "RUNNING">Nul&&(
Echo "%ServiceName%" wont start
)
echo "%ServiceName%" started
)||(
echo "%ServiceName%" was working and stopping
echo
Net stop "%ServiceName%"
)
pause
答案 11 :(得分:0)
与@DanielSerrano的回答有关,最近我对sc.exe
命令的本地化(即西班牙语)感到有些困惑。我的建议是查明保存数字服务状态的行和令牌并对其进行解释,这应该更加健壮:
@echo off
rem TODO: change to the desired service name
set TARGET_SERVICE=w32time
set SERVICE_STATE=
rem Surgically target third line, as some locales (such as Spanish) translated the utility's output
for /F "skip=3 tokens=3" %%i in ('""%windir%\system32\sc.exe" query "%TARGET_SERVICE%" 2>nul"') do (
if not defined SERVICE_STATE set SERVICE_STATE=%%i
)
rem Process result
if not defined SERVICE_STATE (
echo ERROR: could not obtain service state!
) else (
rem NOTE: values correspond to "SERVICE_STATUS.dwCurrentState"
rem https://msdn.microsoft.com/en-us/library/windows/desktop/ms685996(v=vs.85).aspx
if not %SERVICE_STATE%==4 (
echo WARNING: service is not running
rem TODO: perform desired operation
rem net start "%TARGET_SERVICE%"
) else (
echo INFORMATION: service is running
)
)
经过测试:
答案 12 :(得分:0)
也许更简单的方法?只需在此处添加答案列表即可:
@for /f "tokens=1,* delims=: " %%a in ('sc queryex state=Inactive') do net start "%%b"
答案 13 :(得分:0)
对于Windows Server 2012,以下是对我有用的。用实际的服务名称仅替换“ SERVICENAME”:
@ECHO OFF
SET SvcName = SERVICENAME
SC QUERYEX“%SvcName%” |查找“州” |查找/ v“正在运行”> NUL &&( ECHO%SvcName%未运行 回声开始%SvcName%
NET START "%SvcName%" > NUL || (
ECHO "%SvcName%" wont start
EXIT /B 1
)
ECHO "%SvcName%" is started
EXIT /B 0
)|| ( ECHO“%SvcName%”正在运行 退出/ B 0 )