Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Tewr/c80cdfb2949d0631154e to your computer and use it in GitHub Desktop.
Save Tewr/c80cdfb2949d0631154e to your computer and use it in GitHub Desktop.
Batch, log and task scheduler export for restarting a windows service that has gone down for whatever reason (on event 0 + random checks every 30 minutes)
@echo off
setlocal
set LogPath=<logpath>
NET START "SERVICE_NAME" > TEMP_netstartout.tmp 2>&1
set /p NETSTARTOUT= < TEMP_netstartout.tmp
rem 2182 means "service already started", so we'll check for that
type TEMP_netstartout.tmp | FIND "2182" > TEMP_findout.tmp
set /p FINDOUTPUT= < TEMP_findout.tmp
del TEMP_netstartout.tmp
del TEMP_findout.tmp
IF NOT "%FINDOUTPUT%"=="" GOTO :nolog
rem Service was not already started, so log output.
set LogFileExt=.log
set LogFileName=<log_filename_prefix>%LogFileExt%
set MyLogFile=%date%
set MyLogFile=%MyLogFile:/=-%
set MyLogFile=%LogPath%%MyLogFile%_%LogFileName%
echo %time% NET START "SERVICE_NAME": %NETSTARTOUT% >> "%MyLogFile%"
:nolog
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2014-02-20T16:20:55.1120404</Date>
<Author>[account]</Author>
<Description>Attempts to start SERVICE_NAME. Logs the attempt to start it to a log file if the service was not started.</Description>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Application"&gt;&lt;Select Path="Application"&gt;*[System[Provider[@Name='SERVICE_NAME'] and EventID=0]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
</EventTrigger>
<CalendarTrigger>
<Repetition>
<Interval>PT30M</Interval>
<StopAtDurationEnd>false</StopAtDurationEnd>
</Repetition>
<StartBoundary>2014-02-21T06:00:23</StartBoundary>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
<BootTrigger>
<Enabled>true</Enabled>
<Delay>PT15M</Delay>
</BootTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>[SERVICE_ACCOUNT]</UserId>
<LogonType>Password</LogonType>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>P3D</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>[path-to-batch]\restart_service_with_log.bat</Command>
<WorkingDirectory>[path-to-batch]\</WorkingDirectory>
</Exec>
</Actions>
</Task>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment