<p>when a SIGTERM is received from k8s I want my sidecar to die only after the main container is finished. How do I create a dependency chain for shutdown routine?</p>
<p>I can use a <code>preStop</code> and wait for say 120 secs, but it is going to a constant 120 secs for the pods to be cleaned up.</p>
<p>I am wondering if there is a way for my side car to check if my main container was killed. Or is it possible for the main container to signal my side car when it has finished its clean up (through k8s instead of a code change in my main container.</p>

RandomQuests
<p>In your <code>preStop</code> hook, check if your main container is still running (instead of just waiting a fixed amount of time.</p>
<p>If the main container has an existing healthprobe (for example an http endpoint) you can call that from within the <code>preStop</code> hook.</p>
<p>Another option is to edit the entrypoint script in your main container to include a <a href="https://www.linuxjournal.com/content/bash-trap-command" rel="nofollow noreferrer">trap</a> command that creates a file on shutdown. The <code>preStop</code> hook  of your sidecar to wait for this file be present.</p>
<pre><code>trap &quot;touch /lifecycle/main-pod-terminated&quot; SIGTERM
</code></pre>
<p>If using the preStop hook, just <a href="https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks" rel="nofollow noreferrer">keep the grace period countdown in mind</a> and increase the default (30 seconds) if required by setting an appropriate value of <code>terminationGracePeriodSeconds</code></p>
<blockquote>
<p><strong>Pod's termination grace period countdown begins before the <code>preStop</code>
hook is executed</strong>, so regardless of the outcome of the handler, the
container will eventually terminate within the Pod's termination grace
period</p>
</blockquote>
