Platforms such as Kubernetes have support for liveness and readiness probes: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes
Basically, the (web) application needs to provide a specific http endpoint that is called every few seconds by the platform. If the application is unhealthy (typically indicated by a 5xx error) the platform restarts the application.
How does Heroku handle this?