How to negate exit code status to be used in Kubernetes livenessProbe?

Viewed 735

How to negate exit code status to be used in Kubernetes livenessProbe?

I'll be using the grep command and I would like to do below as a check.

  • Return exit value 0, if grep has no hit
  • Return exit value 1, if grep has a hit

Since normally, grep will return 0 if there is a hit, can I negate this something like shown below?

!$(cat filet.txt | grep keyword)
1 Answers

Yes you can give it try

example :

    livenessProbe:
  exec:
    command:
    - /bin/bash
    - -c
    - cat filet.txt | grep keyword
  initialDelaySeconds: 10
  periodSeconds: 10

you should checkout if helpful

-v, --invert-match
          Invert the sense of matching, to select non-matching lines.

You can also try -c

echo 'Test' | grep -c T

1

echo 'Test' | grep -c N

0

shell script

#!/bin/sh
if [ $(echo 'Test' | grep -c T) ]; then
  exit 1
else
  exit 0
fi

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.

livenessProbe:
  exec:
    command:
    - /bin/sh
    - -c
    - /home/test/health.sh
Related