How to send API when server closes?

Is there a way to send trigger something right before a server closes? I am trying to API to my website right when the server closes to show that the server is offline. I know how to do the API but am wondering how to trigger it when the server closes. Any help would be appreciated.

I don’t know exactly what you want to use it for, but what you can do is ping the server and at X number of failed pings explain the server offline.

I think it is possible to send a signal to the API as soon as the server is turning off.
Only is not possible if the server crashed.

What you can do is to monitor a process and as soon as that process is no longer active, the server is turned off.

Wouldn’t it be easier to just ping the server and port and see if its online?

I don’t believe that there is something that can trigger an event/http request right before a server shuts down