如何在手动停止时自动重新启动Windows服务,并在5秒内完成?

huangapple go评论102阅读模式
英文:

how to restart the windows service automatically with 5 seconds, if it is stopped manually?

问题

我有一个应该在客户系统上24*7运行的Windows服务(本地服务器系统)。如果客户试图手动停止服务,我如何在5-10秒内调用该服务?我已经将启动类型设置为自动。但是,这个功能只在系统重新启动时有效。

如果服务被手动停止,需要在5-10秒内自动重新启动服务。

英文:

I have a windows service that should run on the client's system 24*7 (local server system). If the client tries to stop the service manually, how do I invoke the service within 5-10 seconds? I have already placed the startup type as automatic. But, this feature works only when the system is restarted.

Need to restart the service automatically with 5-10 seconds if it is manually stopped.

答案1

得分: 1

你可以设置Windows任务计划程序来检查服务的状态,并在服务停止时重新启动它。以下是步骤:

  1. 按下Windows键 + R,输入“taskschd.msc”并按Enter,打开任务计划程序。

  2. 在右侧窗格中点击“创建任务”。

  3. 给任务命名,比如“服务重启”。

  4. 在“常规”选项卡下,确保选择了“无论用户是否登录都运行”。

  5. 在“触发器”选项卡下,点击“新建”,设置触发任务的事件如下:

    • 日志:系统
    • 来源:服务控制管理器
    • 事件ID:7036
    • 延迟:5秒(或者您喜欢的延迟时间)
  6. 在“操作”选项卡下,点击“新建”,设置运行以下命令的动作:

    • 程序/脚本:“cmd.exe”
    • 添加参数:“/c net start <service_name>”(注意替换<service_name>为您要重新启动的服务名)
  7. 在“条件”选项卡下,取消选中所有复选框。

  8. 在“设置”选项卡下,选择“允许按需运行任务”和“在错过计划开始后尽快运行任务”。

点击“确定”保存任务。现在,当服务被手动停止时,它将在Windows事件查看器中触发一个事件,进而触发您刚刚创建的任务。该任务将等待指定的延迟时间,然后尝试启动服务。

英文:

You can set up a Windows Task Scheduler to check for the status of the service and restart it if it is stopped. Here are the steps:

  1. Open Task Scheduler by pressing the Windows key + R, typing "taskschd.msc" and press Enter.

  2. Click on "Create Task" in the right-hand pane.

  3. Name the task something like "Service Restart".

  4. Under the "General" tab, make sure "Run whether the user is logged on or not" is selected.

  5. Under the "Triggers" tab, click "New" and set up a trigger to start the task "On an event" with the following settings:

  • Log: System
  • Source: Service Control Manager
  • Event ID: 7036
  • Delay: 5 seconds (or whatever delay you prefer)
  1. Under the "Actions" tab, click "New" and set up an action to run the following command:
  • Program/script: "cmd.exe"
  • Add arguments: "/c net start <service_name>"
    imp: Replace <service_name> with the service name you want to restart.
  1. Under the "Conditions" tab, uncheck all boxes.

Under the "Settings" tab, select "Allow task to be run on demand" and "Run task as soon as possible after a scheduled start is missed".

Click "OK" to save the task.

Now, when the service is stopped manually, it will trigger an event in the Windows Event Viewer, which will in turn trigger the task you just created. The task will then wait for the specified delay and attempt to start the service.

答案2

得分: 0

  • 选项A:

编写另一个服务,定期监视该服务,并在其停止时启动它。

  • 选项B:

编写一个脚本/应用程序,监视该服务,如果它停止了就启动它,并定期从任务计划程序中运行该脚本/应用程序。

英文:
  • Option A:

    Write another service that will watch periodically for that service and start it, if it has stopped.

  • Option B:

    Write a script/application that will watch for that service and start it, if it has stopped and run that script/application periodically from the Task Scheduler

答案3

得分: 0

尝试使用Windows任务计划程序。当服务将被停止时,您可以使用EventLog.WriteEntry编写Windows日志条目。该日志条目可用作计划程序中的触发器,并使用NET START命令重新启动服务。

英文:

Try it with the windows task scheduler. You could write an windows log entry when the service will be stopped with EventLog.WriteEntry. This log entry can be used as a trigger in the scheduler and start the service with the NET START command again.

huangapple
  • 本文由 发表于 2023年3月8日 14:34:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定