【问题标题】:Powershell script to detect Apache server failure and initiate restart用于检测 Apache 服务器故障并启动重启的 Powershell 脚本
【发布时间】:2017-11-03 08:11:29
【问题描述】:

我是一名 Java 程序员,我得到了一项任务来找出 Apache 服务器失败/超时的原因,因为它似乎每天至少发生 5-10 次。

我正在查看日志并找到了这些

File does not exist: C:/Apache2/htdocs/favicon.ico
File does not exist: C:/Apache2/htdocs/browserconfig.xml
File does not exist: C:/Apache2/htdocs/selfupdate
Invalid URI in request GET /../../../../../../../../../../../
Invalid method in request ABCD / HTTP/1.1
Invalid method in request QUALYS / HTTP/1.1

时不时会有新事物发生,而且每天都会发生。我用谷歌搜索并尝试了一些修复,但日志中似乎出现了一些新内容。

我确实在第一次失败、第二次失败和后续失败的属性中将恢复选项卡选项设置为“重新启动服务”,然后检查启用选项以停止出现错误。

我决定编写一个 PowerShell 脚本来检查日志并在失败时重新启动 Apache 服务器。

$services = Get-Service -Name Apache2.2

Get-Service | ? {
  $services -contains $_.Name -and $_.fFailureActionsOnNonCrashFailures
-eq ‘True’
} | Restart-Service

运行 Get-Service -Name "Apache2.2" |选择对象-属性 *

给我

    Name : Apache2.2 
    RequiredServices : {Afd, Tcpip} 
    CanPauseAndContinue : False 
    CanShutdown : False 
    CanStop : True 
    DisplayName : Apache2.2 
    DependentServices : {} 
    MachineName : . 
    ServiceName : Apache2.2 
    ServicesDependedOn : {Afd, Tcpip} 
    ServiceHandle : 
    Status : Running 
    ServiceType : Win32OwnProcess 
    Site : 
    Container :

请帮忙。

我现在正在编写一个 java 程序(JAR 文件),它将以管理员身份启动 PowerShell 并运行脚本以重新启动 Apache。然后通过任务计划程序将此 JAR 文件设置为每 3 分钟运行一次。

我想知道如何通过 PowerShell 来检查故障,而不是盲目重启。任何更好的解决方案/建议将不胜感激。

【问题讨论】:

  • 首先我不知道apache服务,但是服务真的有fFailureActionsOnNonCrashFailures这个属性吗?您可以使用此 get-service -name "Apache" 进行检查
  • @guiwhatsthat 即使我不确定,我也从微软论坛 SERVICE_FAILURE_ACTIONS_FLAG 中获取了 fFailureActionsOnNonCrashFailures。 get-service -name Apache2.2 给了我它的显示名称和状态。
  • 与其盲目重启服务,不如找出它挂起的原因并解决问题。
  • 当 Get-Service -Name "YOur service" | Select-Object -Property * 不显示您的 service_failure... 属性,那么您使用的命令对您没有帮助
  • @Tomalak 我明白,这是一种解决方法,直到我找出原因为止。

标签: powershell windows-server-2012 windows-server-2012-r2


【解决方案1】:
What I would do is find the correlating event in the event logs, for when the service stops.  Then right click and create a triggered script or email notification.  The triggered PowerShell script would look like this.

$serviceCheck = Get-Service -Name Apache 2.2 | select status

if ($serviceCheck -eq "Running")
{
    #donothing
}


else
{

  #send email here and restart service

}


Another option would be to run the script as a function and set a sleep timer so it only runs every 5 minutes.  That would look like this:

function Check-Service{

$serviceCheck = Get-Service -Name Apache 2.2 | select status

    if ($serviceCheck.Status -eq "Running")
            {
                          #donothing
            }


    else
            {

                  #send email here and restart service

                 #you can also send an email from here, just make sure you know the smtp server address

            }


        }

【讨论】:

  • 感谢您的回复戴夫。这里的问题,我需要检查服务失败而不是状态。服务失败,但状态保持运行。日志显示上述错误。
  • 然后从与失败相关的事件日志中在任务计划程序中设置电子邮件通知警报。这应该会有所帮助。
  • “与故障相关”如何?当前系统已经向我们发送了通知。由于圣诞节期间,我们需要减少人工干预的需要。服务必须自动重启以减少停机时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
  • 1970-01-01
  • 2020-03-07
  • 2021-03-24
  • 1970-01-01
  • 1970-01-01
  • 2016-04-12
相关资源
最近更新 更多