Detect a service restart and trigger a script

Arosh Wijepala 161 Reputation points
2022-05-19T01:23:53.89+00:00

Hello,

I have developed a script to detect a service stop event by attaching a scheduled task to the eventlog, event ID 7036 and to run a Powershell script upon detecting event id 7036. However, I later realized that event id is not unique to this service and it's written to eventlog by service control manager for every service that is getting started or stopped (not only service stopped event)

For this reason, the script misses execution if the service is quick to get stopped and get started back up again. Also it's getting missed as there are several service restarts by several applications written to the event log at a given time.

Anyone can help me with the logic to detect this kind of a service restart? I am looking to monitor SQL Server service restarts.

Thanks in advance.

Windows for business Windows Server User experience PowerShell
SQL Server Other
{count} votes

5 answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2022-05-19T05:57:55.993+00:00

    I am looking to monitor SQL Server service restarts.

    Review / analyse the SQL Server ErrorLog: https://learn.microsoft.com/en-us/sql/tools/configuration-manager/viewing-the-sql-server-error-log?view=sql-server-ver15

    Before you invest a lot of time: Even with the information from ErrorLog, you will never get in 100% of cases the cause.

    0 comments No comments

  2. Bjoern Peters 8,921 Reputation points
    2022-05-19T06:59:46.627+00:00

    What do you want to monitor?

    Simply, if the SQL Server DB engine services have been restarted? So you want to trigger a PowerShell script that fires an event in your monitoring solution after a restart?

    Why not use the SQL Server Agent?
    Creating a Job/Step which runs only on SQL Server Agent start ...

    Agent always stops/starts depending on DB engine... => so job trigger/schedule would be => "Start automatically when SQL Server Agent starts"
    https://learn.microsoft.com/en-us/sql/ssms/agent/schedule-a-job?view=sql-server-ver15#to-create-and-attach-a-schedule-to-a-job

    and then have a step that executes PowerShell
    https://learn.microsoft.com/en-us/sql/ssms/agent/create-a-powershell-script-job-step?view=sql-server-ver15

    0 comments No comments

  3. Seeya Xi-MSFT 16,586 Reputation points
    2022-05-19T10:19:42.79+00:00

    Hi @Arosh Wijepala ,

    Welcome to Microsoft Q&A!
    Could you try to use the event 17162 in Windows Event.
    Click on Filter current log in the right-pan of the event viewer:
    203648-image.png
    Select all event levels, specify the 17162 event ID in the event ID text box and click OK:
    203673-image.png
    Here is my result:
    203702-image.png
    And i did a test that i restarted SQL Server and refreshed it, then a new event 17162 come out.

    Best regards,
    Seeya


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  4. Tom Phillips 17,771 Reputation points
    2022-05-19T16:37:30.033+00:00

    The simplest way to accomplish is would be with a startup proc.

    Please see:
    https://www.mssqltips.com/sqlservertip/1574/automatically-running-stored-procedures-at-sql-server-startup/

    0 comments No comments

  5. Limitless Technology 39,916 Reputation points
    2022-05-25T07:28:22.087+00:00

    Hi there,

    -Open the SQL Server Configuration Manager
    -Open event viewer -> windows log -> application -> sort the information by date
    -Using the SQL Server Configuration Manager Stop the server or perform the events that you need the script to catch.
    -With these event ID you can modify the script . You can also use other tools to get the events.

    Process Monitor is an advanced monitoring tool for Windows that shows real-time file system, Registry and process/thread activity. You can get the tool from here https://learn.microsoft.com/en-us/sysinternals/downloads/procmon

    ---------------------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.