0

问题是我有一些 Java 进程不时挂起,需要停止、清理然后重新启动。我已经拨入了整个过程的后半部分,但是在尝试实际停止服务时,我遇到了一些问题。正如您将在下面看到的那样,我首先强制停止服务,然后一旦它“完成”它会等待一段时间,然后检查服务的状态,然后继续寻找我们可以开始杀死的相关进程,但我的脚本似乎跳了快速进入检查服务是否已停止。我的 sleep 语句似乎没有一个值可以在我的所有主机上运行,​​甚至在另一个服务重启期间也可以在同一台主机上运行。

$service = "arc_connector_2"
# Force stopping the service before checking and allowing user to kill individual processes 
 related to the service. 

Write-Host("Stopping " + $service)
Stop-Service -Name $service -Force -PassThru


Start-Sleep -Seconds 20
# Validating the service stopped and if not then the ability to kill individual processes kick in.
$servchk = Get-Service -Name $service
if ($servchk.Status -ne 'Stopped'){
    Get-WmiObject win32_process -Filter "name='wrapper.exe'" | Format-List -Property Name, 
Path, ProcessID # Queries the host for any wrapper.exe's if service fails to stop.
    Write-Host ""
    $wp = read-host -Prompt "Enter the process ID of the connector asssociated with this 
wrapper process" 
    Write-Host ""
    Get-WmiObject win32_process -Filter "name='java.exe'" | Format-List -Property Name, Path, 
ProcessID # Queries the host for any java.exe's if service fails to stop. 
    Write-Host ""
    $jp = Read-Host -Prompt "Enter the process ID of the connector associated with this java 
process"
    # Killing the process with a confirmation prompt so the user doesn't mistakenly kill the wrong service.
    Stop-Process -Id $wp -Confirm -PassThru

    # Killing the process with a confirmation prompt so the user doesn't mistakenly kill the wrong service.
    Stop-Process -Id $jp -Confirm -PassThru
    Start-Sleep -Seconds 5
}
4

0 回答 0