用于重启 Apache 进程的 Powershell 脚本

Powershell script to restart Apache Process

我正在使用 powershell 脚本在 Apache 进程达到内存阈值后重新启动 Apache 服务。 该脚本将按照任务管理器中的计划每分钟 运行 2-3 次。

在下面的脚本中,我是否需要使用 start-sleep commandlet?如果需要,最好将其放置在何处以及使其休眠多少秒?

$Threshold = 200 * 1024 * 1024;     # Bytes (209,715,200 bytes = 0.195GB)
$ServiceName = my service name;

Get-WmiObject -ComputerName "localhost" -Class Win32_PerfFormattedData_PerfProc_Process | where { $_.Name -like "httpd*" } | foreach {
    $procobj = $_;

    if($procobj.workingset -gt $Threshold)
    {  
        stop-Service $ServiceName;

        start-Service $ServiceName;

        $stamp = Get-Date
        echo $($stamp + " Started service: " + $ServiceName);
        #sleep 5;  //Do I need this and if so, where should it be placed?
    }
}

更新 Apache 可能有 2 个或更多进程,Get-Wmi 将 return 它们作为 httpd、httpd#1、httpd#2、....
我问这个的原因是循环将检查这些进程的工作集,所以假设有 3 个已处理的 returned:httpd、httpd#1 和 httpd#2 并成像 httpd#1 和 httpd# 2 已达到阈值。 请注意,在循环中,我正在重新启动 service 这将重新启动进程。由于我的 2 个 httpd 进程已达到阈值,我将重新启动服务 2-ice。我试图确保即使多个进程已达到阈值,服务重启也只会发生一次。不确定 start-sleep 是否可以解决这个问题,或者是否有更好的方法。

非常感谢。

$Threshold = 200 * 1024 * 1024;     # Bytes (209,715,200 bytes = 0.195GB)
$ServiceName = my service name;

Get-WmiObject -ComputerName "localhost" -Class Win32_PerfFormattedData_PerfProc_Process | where { $_.Name -like "httpd*" } | foreach {
    $procobj = $_;

    if($procobj.workingset -gt $Threshold)
    {  
        stop-Service $ServiceName;

        start-Service $ServiceName;

        $stamp = Get-Date
        echo $($stamp + " Started service: " + $ServiceName);
        break;
    }
}