<p>A simplified version of the code</p>

<pre><code>trap 'rm /tmp/not_done; echo "trap command got executed"; sleep 10' SIGTERM

wait_sigterm() {

  touch /tmp/not_done
  while [ -f /tmp/not_done ]
  do
      sleep 60 &amp;
      wait $!  
  done
}

wait_sigterm
</code></pre>

<p>How can I make sure trap command got executed?</p>

<p>One way would be if I can get access to the logs of the killed container and check for my echo message.</p>

<p>I tried getting the logs (kubectl -n namespace-name logs pod-name container-name) in a while loop, so that I would be able to pick the last written logs till the container is alive.</p>

<p>But the echo message was not present.
My guess is logs are not written once we kill the container or maybe something else.
Is there any way I can get the logs of a killed container or test the above scenario?</p>

user7131571
<p><strong>Approach 1</strong></p>

<p>If you want to check out the logs of the previously terminated container from pod POD_NAME.
You can use the following command with -p flag</p>

<pre><code>kubectl logs POD_NAME -c CONTAINER_NAME -p
</code></pre>

<p>You can get further information such as <strong>options and flags</strong>  with the following command </p>

<pre><code>kubectl logs --help
</code></pre>

<p><strong>Approach 2</strong></p>

<p>another approach to determine the termination message is in the Status field of Pod object. </p>

<p><code>kubectl get pod $POD_NAME -o yaml</code></p>

<p>check the field <strong>lastState</strong> of <strong>containerStatuses</strong>:</p>

<pre><code>status:
  conditions:

  containerStatuses:
  - containerID: 
    image: registry.access.redhat.com/rhel7:latest
    imageID:
    lastState: {}

    state:
      running:

</code></pre>

<p>Here is more detail <a href="https://kubernetes.io/docs/tasks/debug-application-cluster/determine-reason-pod-failure/#writing-and-reading-a-termination-message" rel="nofollow noreferrer">reading-a-termination-message</a></p>
