<p>Note: this is more a bash/shell script issue than Kubernetes.</p>
<p>So I have one replica of a pod, and so <code>kubectl get pods</code> will only return one active replica.</p>
<p>Conceptually what I want to do is simple.  I want to do a <code>kubectl logs my-pod-nnn1</code> with a watcher, HOWEVER when that pod terminates I want to stream a message &quot;my-pod-nnn1 terminated, now logging my-pod-nnn2&quot;, and then stream the new logs.  This is a fairly complex process (for me) and I'm wondering what approach I could take, and if this is possible (or perhaps not necessary) with multi-threading of some kind (which I have not done). Thanks</p>

Oliver Williams
<p>as a rough outline for what you'll need to do, if I've read your needs right</p>
<pre class="lang-sh prettyprint-override"><code>slm-log-continue() {
  while true ; do
    PODLINE=$(kubectl get pod | grep regional-wkspce | grep Running | awk '/^regional-wkspce-.*/{print $1}')
    if [[ -z &quot;$PODLINE&quot; ]]; then
        echo no pod currently active, waiting 5 seconds ...
        sleep 5
    else
        echo &quot;----- logging for $PODLINE -----&quot;
        kubectl logs -f $PODLINE
        echo &quot;----- pod $PODLINE disconnected -----&quot;
    fi
  done
}
</code></pre>
<p>assuming kubectl logs terminates after the pod does and it's received the end of the logs ( I've not tested ), something like that should do what you need without any fancy multithreading. it will just find the current pod using whatever regex against the <code>get pods</code> output, extract the name and then spit out logs until it dies.</p>
