<p>I am working on a spring boot application and we deploy through kubernetes. My requirement is to run some logic in case the pod crashes or pod gets removed or pod is intentionally shut down. Currently I am using @<strong>PreDestroy</strong> to run my logics on exit.</p>

<pre><code>@Component
public class EngineShutDownHook{

private static final Logger LOGGER = LoggerFactory.getLogger(EngineShutDownHook.class);

@PreDestroy
public void onExit() {
    LOGGER.info("Shutting engine.");
    System.out.println(" engine is stopping.");
}
}
</code></pre>

<p>However I am not sure whether this code will run on all possible exit scenarios. I have also learnt about spring's  <strong>ExitCodeGenerator</strong>. Can you please suggest which is the best way to achieve this ?</p>

smruti ranjan
<p>Use <a href="https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/" rel="nofollow noreferrer">Container Lifecycle Hooks</a> of K8s</p>

<p><strong>PreStop:</strong> This hook is called immediately before a container is terminated due to an API request or management event such as liveness probe failure, preemption, resource contention and others.</p>
