首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于检测Apache服务器故障并启动重启的Powershell脚本

用于检测Apache服务器故障并启动重启的Powershell脚本
EN

Stack Overflow用户
提问于 2017-11-03 16:11:30
回答 1查看 985关注 0票数 0

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

我看了一下日志发现了这些

代码语言:javascript
运行
复制
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

时不时会有新的事情发生,每天都会发生。我在谷歌上搜索并尝试了一些修复,但在日志中似乎出现了一些新的东西。

我确实在第一次失败、第二次失败和后续失败时将属性中的recovery选项卡选项设置为"Restart the service“,然后选中了stops errors的启用选项。

我决定编写一个PowerShell脚本来检查日志,并在服务器出现故障时重新启动它。

代码语言:javascript
运行
复制
$services = Get-Service -Name Apache2.2

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

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

给了我

代码语言:javascript
运行
复制
    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。然后通过Task Scheduler将这个JAR文件设置为每3分钟运行一次。

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

EN

回答 1

Stack Overflow用户

发布于 2017-11-06 05:11:39

代码语言:javascript
运行
复制
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

            }


        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47091108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档