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.
3
Answers
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
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 theNET START
command again.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:
imp: Replace <service_name> with the service name you want to restart.
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.