Wednesday, August 28, 2024

Windows: Script for restarting the service with additional checking and logging to the event log

This script is useful if you need to regularly restart one of the Windows services according to the scheduler. If the service fails to start, a new attempt will be made to start the service after one minute. In this case, the script writes notifications about successful and unsuccessful startup attempts to the "Application" log.

@echo off
net stop MyService
:try
net start MyService
if %ERRORLEVEL% NEQ 0 goto error
eventcreate /t information /l application /id 100 /d "Service MyService started by script successfully."
exit
:error
eventcreate /t error /l application /id 100 /d "Service MyService couldn't start by script. Retry in 60 seconds..."
timeout 60 > NUL
goto try


No comments:

Post a Comment