How to detect when a Windows service stops and starts automatically? Automatically run .bat batch file?

Table of contents

Table of contents

Preface

1. The detection service stops and starts automatically

1.1-Thinking

2.2-Source code

2. Automatically run the .bat batch file

2.1-Preparation

2.2-Create task executor


Author: Xigua Programmer

Home page portal:Xigua Programmer_ASP.NET, ASP.NET Core, Server-CSDN Blog

Preface

Want to make sure your Windows services automatically restart even after crashing? This article teaches you how to do this with a small batch script. Teach you how to set up scheduled checks. If you find that the service is down, pull it up immediately. Follow the steps and making your service run stably is no longer a problem!

One day, a developer wrote a message queue consumer. After deploying it to a Windows service, he found that the service would stop from time to time for no reason. There was neither log nor any event notification, which was very strange. On a dark night, he found [Xigua Programmer] and asked him to help him write a program to automatically start and run Windows services. This is what happened!

1. The detection service stops and starts automatically

1.1-Thinking

The main thing here is to write a .bat batch file, and then monitor a specific Windows service in a loop. If the service stops, try to restart it, and detect the status of the service every N seconds (customized). Then you need to keep running this batch file.

2.2-Source code

Create a new [xxx.bat] file in your computer/server, and then write the following code. The bat file can be directly double-clicked to run. After writing, you can run it yourself to see the effect.

@echo off
rem 定义循环间隔时间和监测的服务:
set secs=60
set srvname="服务名"
 
echo.
echo ========================================
echo ==         查询计算机服务的状态,     ==
echo ==     每间隔%secs%秒种进行一次查询,     ==
echo ==     如发现其停止,则立即启动。     ==
echo ========================================
echo.
echo 此脚本监测的服务是:%srvname%
echo.
 
if %srvname%. == . goto end
 
:chkit
set svrst=0
for /F "tokens=1* delims= " %%a in ('net start') do if /I "%%a %%b" == %srvname% set svrst=1
if %svrst% == 0 net start %srvname%
set svrst=
rem 下面的命令用于延时,否则可能会导致cpu单个核心满载。
ping -n %secs% 127.0.0.1 > nul
goto chkit
 
:end

Notice:

Screenshot of successful operation (it doesn’t matter if garbled characters appear):

2. Automatically run the .bat batch file

We know that the .bat batch file that detects whether the service has stopped needs to be running all the time. That is, as long as the black window is closed and the program is called, the program will stop. So we need to put this .bat file in the background and keep running. [Xigua Programmer] Here we take deployment in [Task Scheduler] as an example to let the .bat file automatically execute every 1 minute. Then it is executed on the Windows Server server (the operation on the PC is slightly different).

2.1-Preparation

The following configuration must be performed in the early stage, otherwise it may be reported that "the operator or system administrator rejected the request." (Ox800710EO)" and other errors (if not configured, it may not be executed: the scheduled task is not configured in a valid group, or the user group where the scheduled task is located is invalid or does not have permissions). It is best to restart the computer/server after configuration. , otherwise it may not be effective.

(1) First enter [win+R keys, then enter gpedit.msc] or search directly for [Local Security Policy].

(2) Right-click [Local Policy——>User Rights Assignment——>Allow Local Login], and then click [Properties].

Then click [Add User or Group], enter [Administrator] in the input object name to select input box, and then click [Check Name]. There may be some extra prefixes or underlines. It doesn't matter, it means it has been detected. Then click [Confirm].

(3) Right-click [Local Policy——>User Rights Assignment——>Login as Batch File], then click [Properties], and then follow the same steps as above.

(4) Right-click [Local Policy -> Security Options -> Domain Controller: Allow server operators to schedule tasks], and then click [Properties]

Then select [Enabled], and finally click [OK].

2.2-Create task executor

(1) Open [Task Scheduler].

(2) Then click [Create Task], in the [General] tab option, enter [Name], check [Run regardless of whether the user is logged in], and check [Run with highest privileges], otherwise it may result in no permission to execute. The task.

(3) In the [Trigger] tab option, click [New] to create a trigger, that is, when to execute this task. [Xigua Programmer] is executed every 1 minute and the duration is indefinite.

(4) In the [Operation] tab option, click [New] to create a trigger, which is the specific task to be performed. [Xigua Programmer] Take executing a .bat file as an example.

(5) In the [Conditions] tab option, uncheck [Only start this task when the computer is using AC power].

(6) In the [Settings] tab option, if this task has already been run, the following rules apply [Stop existing instances] (select according to your needs), and finally click [Confirm] to create the task.

(7) Finally, just wait for the execution to start, or you can click [Run] manually.

Copyright statement: This article is an original article, and the copyright belongs to [Xigua Programmer]. Please indicate the source when reprinting. If you have any questions, please send a private message.

Original link:How to detect when a Windows service stops and starts automatically? Automatically run .bat batch file? -CSDN Blog

Guess you like

Origin blog.csdn.net/2301_79251107/article/details/134805828