英文:
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任务计划程序来检查服务的状态,并在服务停止时重新启动它。以下是步骤:
-
按下Windows键 + R,输入“taskschd.msc”并按Enter,打开任务计划程序。
-
在右侧窗格中点击“创建任务”。
-
给任务命名,比如“服务重启”。
-
在“常规”选项卡下,确保选择了“无论用户是否登录都运行”。
-
在“触发器”选项卡下,点击“新建”,设置触发任务的事件如下:
- 日志:系统
- 来源:服务控制管理器
- 事件ID:7036
- 延迟:5秒(或者您喜欢的延迟时间)
-
在“操作”选项卡下,点击“新建”,设置运行以下命令的动作:
- 程序/脚本:“cmd.exe”
- 添加参数:“/c net start <service_name>”(注意替换<service_name>为您要重新启动的服务名)
-
在“条件”选项卡下,取消选中所有复选框。
-
在“设置”选项卡下,选择“允许按需运行任务”和“在错过计划开始后尽快运行任务”。
点击“确定”保存任务。现在,当服务被手动停止时,它将在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:
-
Open Task Scheduler by pressing the Windows key + R, typing "taskschd.msc" and press Enter.
-
Click on "Create Task" in the right-hand pane.
-
Name the task something like "Service Restart".
-
Under the "General" tab, make sure "Run whether the user is logged on or not" is selected.
-
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)
- 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.
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论