<p>I've read the <a href="http://kubernetes.io/v1.0/docs/user-guide/production-pods.html#lifecycle-hooks-and-termination-notice" rel="nofollow">docs</a> on graceful termination of a pod in Kubernetes but I'm not quite sure how to map my specific use case of shutting down a Java process gracefully.</p>

<p>What I want to do is to run the following bash command as a part of the termination process:</p>

<pre><code>$ kill -SIGTERM `pidof java`
</code></pre>

<p>I've tried this:</p>

<pre><code>...    
lifecycle:
    preStop:
      exec:                
        command: ["kill", "-SIGTERM", "`pidof java`"]
...
</code></pre>

<p>But nothing happens but the Java shutdown hook doesn't seem to kick-in when I stop the pod (<code>kubectl stop pod pod-xxx</code>). I suspect that the <code>pidof java</code> part of the <code>kill</code> command doesn't work (but I'm not sure). How would I do this in Kubernetes?</p>

<p>I started a bash shell inside the container and executed my command instead and that turned out to work:</p>

<pre><code>command: ["/bin/bash", "-c", "PID=`pidof java` &amp;&amp; kill -SIGTERM $PID &amp;&amp; while ps -p $PID &gt; /dev/null; do sleep 1; done;"]
</code></pre>

<p>Without <code>/bin/bash</code> I couldn't get it working.</p>
