<p>I've this command to list the <code>Kubernetes</code> pods that are not running:</p>

<pre><code>sudo kubectl get pods -n my-name-space | grep -v Running
</code></pre>

<p>Is there a command that will return a count of pods that are not running?</p>

<p>If you add <code>... | wc -l</code> to the end of that command, it will print the number of lines that the <code>grep</code> command outputs.  This will probably include the title line, but you can suppress that.</p>

<pre class="lang-sh prettyprint-override"><code>kubectl get pods -n my-name-space --no-headers \
  | grep -v Running \
  | wc -l
</code></pre>

<p>If you have a JSON-processing tool like <a href="https://stedolan.github.io/jq/" rel="noreferrer"><code>jq</code></a> available, you can get more reliable output (the <code>grep</code> invocation will get an incorrect answer if an <code>Evicted</code> pod happens to have the string <code>Running</code> in its name).  You should be able to do something like (untested)</p>

<pre class="lang-sh prettyprint-override"><code>kubectl get pods -n my-namespace -o json \
  | jq '.items | map(select(.status.phase != "Running")) | length'
</code></pre>

<p>If you'll be doing a lot of this, writing a non-shell program using the <a href="https://kubernetes.io/docs/reference/using-api/api-overview/" rel="noreferrer">Kubernetes API</a> will be more robust; you will generally be able to do an operation like "get pods" using an SDK call and get back a list of pod objects that you can filter.</p>
