Sql*_*yan 1 remote-access service
我目前正在使用以下 BAT 文件来重新启动远程服务:
sc \\MyServer stop MyRemoteService
sc \\MyServer start MyRemoteService
Run Code Online (Sandbox Code Playgroud)
这适用于一台计算机,但我想用它来重新启动服务器场中的服务。我可以在批处理文件中列出每个服务器,但我只想重新启动服务,如果它们已经在运行。例如,如果该服务在场中的一台服务器上运行,请重新启动它,但如果它尚未运行,则将其停止。
有没有办法远程完成这个?我没有嫁给 SC 是不是有另一个程序可以做到这一点。
这是一个让我高兴的问题的肮脏答案:)
sc \\MyServer query MyRemoteService | find "RUNNING" && echo Service running - now stopping && sc \\MyServer stop MyRemoteService && ping 127.0.0.1 -n 10 && echo Now starting service && sc \\MyServer start MyRemoteService || echo Service not running
Run Code Online (Sandbox Code Playgroud)
这比 Izzy 的回答更冗长,但允许在“STOP_PENDING”状态下服务更长的持续时间。
假设服务器在命令行上指定的文本文件中,格式为:
SERVERNAME1
SERVERNAME2
...
Run Code Online (Sandbox Code Playgroud)
和脚本:
@echo off
set SVC=ServiceName
if "%1"=="" goto end
for /f %%i in (%1) do call :do_bounce %%i
goto end
:do_bounce
rem Query for service running and bail if not
sc \\%1 query %SVC% | find "RUNNING"
if errorlevel 1 goto end
rem Stop the service and loop checking for it to stop
sc \\%1 stop %SVC%
:check_stopped
ping 127.0.0.1 -n 2 >NUL 2>NUL
sc \\%1 query %SVC% | find "STOPPED"
if errorlevel 1 goto check_stopped
rem Restart the service
sc \\%1 start %SVC%
:end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5102 次 |
| 最近记录: |