<p>How to negate exit code status to be used in <code>Kubernetes</code> <code>livenessProbe</code>?</p>
<p>I'll be using the <code>grep</code> command and I would like to do below as a check.</p>
<ul>
<li>Return exit value 0, if <code>grep</code> has no hit</li>
<li>Return exit value 1, if <code>grep</code> has a hit</li>
</ul>
<p>Since normally, grep will return 0 if there is a hit, can I negate this something like shown below?</p>
<pre><code>!$(cat filet.txt | grep keyword)
</code></pre>

<p>Yes you can give it try</p>
<p>example :</p>
<pre><code>    livenessProbe:
  exec:
    command:
    - /bin/bash
    - -c
    - cat filet.txt | grep keyword
  initialDelaySeconds: 10
  periodSeconds: 10
</code></pre>
<p>you should checkout if helpful</p>
<pre><code>-v, --invert-match
          Invert the sense of matching, to select non-matching lines.
</code></pre>
<p>You can also try <code>-c</code></p>
<pre><code>echo 'Test' | grep -c T
</code></pre>
<p><strong>1</strong></p>
<pre><code>echo 'Test' | grep -c N
</code></pre>
<p><strong>0</strong></p>
<p><strong>shell script</strong></p>
<pre><code>#!/bin/sh
if [ $(echo 'Test' | grep -c T) ]; then
  exit 1
else
  exit 0
fi
</code></pre>
<p>the easiest way would be writing shell script and managing the exit code for liveness as you want 0 or 1 and based on that liveness will restart the pod.</p>
<pre><code>livenessProbe:
  exec:
    command:
    - /bin/sh
    - -c
    - /home/test/health.sh
</code></pre>
