AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 48600
In Process
Liam
Liam
Asked: 2009-08-01 08:25:54 +0800 CST2009-08-01 08:25:54 +0800 CST 2009-08-01 08:25:54 +0800 CST

如果 Windows 服务崩溃,如何自动重新启动它?

  • 772

我有一个 Windows 服务每隔几天就会意外退出。有没有一种简单的方法来监控它以确保它在崩溃时能够快速重启?

windows windows-service
  • 8 8 个回答
  • 192643 Views

8 个回答

  • Voted
  1. Christopher_G_Lewis
    2009-08-01T08:30:32+08:002009-08-01T08:30:32+08:00

    在服务应用程序下,选择相关服务的属性。

    查看恢复选项卡 - 有各种各样的选项 - 我将设置 First & Second Failure 以重新启动服务,Third 以运行批处理程序,BLAT会发送一封带有第三个失败通知的电子邮件。

    您还应该将重置失败计数设置为 1 以每天重置失败计数。

    编辑:

    看起来您可以通过命令行执行此操作:

    SC failure w3svc reset= 432000  actions= restart/30000/restart/60000/run/60000
    SC failure w3svc command= "MyBatchFile.cmd"
    

    您的 MyBatchFile.CMD 文件可能如下所示:

    blat - -body "Service W3svc Failed" -subject "SERVICE ERROR" -to [email protected] -server SMTP.Example.com -f [email protected]
    
    • 94
  2. jeremyasnyder
    2009-08-01T08:30:17+08:002009-08-01T08:30:17+08:00

    打开 Services.msc,双击服务以打开服务的属性,有一个恢复选项卡,这些设置应该允许您在失败时重新启动服务。

    • 10
  3. MSS
    2018-04-24T22:57:12+08:002018-04-24T22:57:12+08:00

    尝试将恢复时间设置为零:

    在此处输入图像描述

    命令行等价物:

    SC failure YOUR_SERVICE_NAME reset= 0 actions= restart/0/restart/0/restart/0

    无论如何,有时自动恢复无法正常工作,建议使用第三方软件。似乎当退出代码为 0 个窗口的服务正常退出时,不会尝试恢复它。

    • 7
  4. Anderson
    2016-12-20T02:53:08+08:002016-12-20T02:53:08+08:00

    如果停止,我有类似的要求来启动服务。我认为最简单的解决方案是每 5 分钟在 windows 任务调度程序中执行以下命令:

    网络启动 MyServiceName

    此命令将基本上启动服务(如果已停止),如果服务已在运行则无效。

    • 2
  5. Play_Park
    2009-10-12T19:43:19+08:002009-10-12T19:43:19+08:00

    我在 HostForLife.eu 的 Windows 2008 服务器上使用ServiceKeeper,它运行良好。之前我对 ServiceHawk 有过评测,但我更喜欢使用 ServiceKeeper,因为它更易于管理和界面。

    • 1
  6. Nick
    2017-02-09T13:50:23+08:002017-02-09T13:50:23+08:00

    我最近实现了一个恢复选项来运行一个 powershell 脚本,该脚本尝试重新启动服务定义的次数,并在结束时发送电子邮件通知,它也.

    经过几次尝试(尽管我已经看到了所有其他事情),服务中恢复选项卡上的字段配置如下:

    程序:Powershell.exe
    **不是 C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe

    命令行参数:-command "& {SomePath\YourScript.ps1 '$args[0]' '$args[1]' '$args[n]'}"

    例如:-command "& {C:\PowershellScripts\ServicesRecovery.ps1 '服务名称'}"

    ** $args 是将传递给您的脚本的参数。这些不是必需的。

    这是powershell脚本:

    cd $PSScriptRoot
    
    $n = $args[0]
    
    function CreateLogFile {
    $events = Get-EventLog -LogName Application -Source SomeSource -Newest 40
    if (!(Test-Path "c:\temp")) {
        New-Item -Path "c:\temp" -Type directory}
    if (!(Test-Path "c:\temp\ServicesLogs.txt")) {
        New-Item -Path "c:\temp" -Type File -Name "ServicesLogs.txt"}
        $events | Out-File -width 600 c:\temp\ServicesLogs.txt
    }
    
    function SendEmail  {
    $EmailServer = "SMTP Server"
    $ToAddress = "[email protected]"
    $FromAddress = "[email protected]"
    
    CreateLogFile
    
    $Retrycount = $Retrycount + 1
    send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service failure" `
    -Body "The $n service on server $env:COMPUTERNAME has stopped and was unable to be restarted after $Retrycount attempts." -Attachments c:\temp\ServicesLogs.txt
    
    Remove-Item "c:\temp\ServicesLogs.txt"
    }
    
    function SendEmailFail  {
    $EmailServer = "SMTP Server"
    $ToAddress = "[email protected]"
    $FromAddress = "[email protected]"
    
    CreateLogFile
    
    $Retrycount = $Retrycount + 1
    send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service Restarted" `
    -Body "The $n service on server $env:COMPUTERNAME stopped and was successfully restarted after $Retrycount attempts. The relevant system logs are attached." -Attachments c:\temp\ServicesLogs.txt
    
    Remove-Item "c:\temp\ServicesLogs.txt"
    }
    
    function StartService {
    
    $Stoploop = $false
    
    do {
       if ($Retrycount -gt 3){
         $Stoploop = $true
         SendEmail
         Break
        }
    
       $i =  Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select Name, State, StartMode
        if ($i.State -ne "Running" -and $i.StartMode -ne "Disabled") {
    
            sc.exe start $n
            Start-Sleep -Seconds 35
    
            $i =  Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select State
              if ($i.state -eq "Running"){
                  $Stoploop = $true
                  SendEmailFail}
              else {$Retrycount = $Retrycount + 1}
        }        
    }
    While ($Stoploop -eq $false)
    }
    
    [int]$Retrycount = "0"
    StartService
    
    • 1
  7. Maxwell
    2009-08-01T08:29:45+08:002009-08-01T08:29:45+08:00

    这是我在类似线程上的回答希望这有帮助......

    如果需要,您可以安排一个像这样的简单 vbs 脚本来定期重新启动计算机上的服务。

    strComputer =“。”
    strSvcName = "YOUR_SERVICE_NAME"
    设置 objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    设置 objService = objWMI.Get("Win32_Service.Name='" & strSvcName & "'")
    如果 objService.State="Stopped" 那么
        objService.StartService()
    万一
    


    • 0
  8. ExtraLean
    2009-10-12T17:10:36+08:002009-10-12T17:10:36+08:00

    有人在超级用户上问过类似的问题:你可以安装一个监控 Windows 服务的工具。Service Hawk之类的东西可以帮助您保持服务启动,或者允许您安排自动重启(可能在夜间)以保持服务平稳运行。

    • 0

相关问题

  • 您最喜欢的云计算提供商是什么?[关闭]

  • Vanilla Powershell 是否足以成为 Windows 和 DB 服务器管理员的语言?

  • 为什么添加新驱动器后我的磁盘驱动器访问速度如此之慢?

  • 在 Windows Server 2003 下使用 wscipt 从 .asp 文件运行 .exe

  • 最佳混合环境(OS X + Windows)备份?[关闭]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve