skip to Main Content

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

    Login or Signup to reply.
  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search