B
B
beduin012021-10-07 11:12:45
System administration
beduin01, 2021-10-07 11:12:45

How to organize a service restart in case of a crash?

I have a web service that manages to crash every few days. If this happens, I'm fine with its automatic restart. I tried the following solution:

@echo off
:startover
echo (%time%) App started.
call ".\bin\server.exe"
echo (%time%) WARNING: Restarting.
goto startover


The problem is that at some point a situation came up when the server was restarted, but the port remained busy and the server began to swear that it could not bind to the port. he is busy.

How this could happen I have no idea. In theory, if the service falls, then it releases the port. But it turned out that for some reason he was not released / was released at the wrong time. Who has any ideas?

How else can you solve the restart problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ronald McDonald, 2021-10-07
@Zoominger

Did this. The script "launching the program if it is not running powershell" is googled for one or two, and it takes two minutes to fasten the logic to correctly finish off the hung service.
In general:
1. Check if the process is running;
2. If not, then check the port;
3. If the port is not closed, then simply restart the service;
4. If the port is closed, then we finish off the remnants of the service and restart.

R
res2001, 2021-10-07
@res2001

In theory, if the service falls, then it releases the port.

It's just your idea.
In fact, the port may still be busy for some time until a packet with the FIN flag set arrives or the timeout for waiting for such a packet expires. This is how the TCP protocol works.
Usually, to avoid waiting for a port to close, the port is opened with the SO_REUSEADDR flag.
Perhaps the program crashes because the connection is broken. Probably at you this situation (if in general is processed) is not correctly processed.
At the expense of restart - it may be worth registering your service as a screw service, then the screw Service Manager will be responsible for the restart. And you don’t need a crutch in the form of a body shirt.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question