<p>I am having difficulty getting a kubernetes livenessProbe exec command to work with environment variables.
My goal is for the liveness probe to monitor memory usage on the pod as well as also perform an httpGet health check.</p>
<p>&quot;If container memory usage exceeds 90% of the resource limits OR the http response code at <code>/health</code> fails then the probe should fail.&quot;</p>
<p>The liveness probe is configured as follows:</p>
<pre><code>
livenessProbe:
  exec:
    command:
    - sh
    - -c
    - |-
      &quot;used=$(awk '{ print int($1/1.049e+6) }' /sys/fs/cgroup/memory/memory.usage_in_bytes);
      thresh=$(awk '{ print int( $1 / 1.049e+6 * 0.9 ) }' /sys/fs/cgroup/memory/memory.limit_in_bytes);
      health=$(curl -s -o /dev/null --write-out &quot;%{http_code}&quot; http://localhost:8080/health);
      if [[ ${used} -gt ${thresh} || ${health} -ne 200 ]]; then exit 1; fi&quot;
  initialDelaySeconds: 240
  periodSeconds: 60
  failureThreshold: 3
  timeoutSeconds: 10

</code></pre>
<p>If I exec into the (ubuntu) pod and run these commands they all work fine and do the job.</p>
<p>But when deployed as a livenessProbe the pod is constantly failing with the following warning:</p>
<pre><code>Events:                                                                                                                                                                                                               │
│   Type     Reason     Age                  From     Message                                                                                                                                                           │
│   ----     ------     ----                 ----     -------                                                                                                                                                           │
│   Warning  Unhealthy  14m (x60 over 159m)  kubelet  (combined from similar events): Liveness probe failed: sh: 4: used=1608;                                                                                          │
│ thresh=2249;                                                                                                                                                                                                          │
│ health=200;                                                                                                                                                                                                           │
│ if [[  -gt  ||  -ne 200 ]]; then exit 1; fi: not found
</code></pre>
<p>It looks as if the initial commands to probe memory and curl the health check endpoint all worked and populated environment variables but then those variable substitutions did not subsequently populate in the if statement so the probe never passes.</p>
<p>Any idea as to why? Or how this could be configured to work properly?
I know it's a little bit convoluted. Thanks in advance.</p>

<p>Looks like the shell is seeing your whole command as a filename to execute.</p>
<p>I would remove the outer quotes</p>
<pre><code>livenessProbe:
  exec:
    command:
    - sh
    - -c
    - |-
      used=$(awk '{ print int($1/1.049e+6) }' /sys/fs/cgroup/memory/memory.usage_in_bytes);
      thresh=$(awk '{ print int( $1 / 1.049e+6 * 0.9 ) }' /sys/fs/cgroup/memory/memory.limit_in_bytes);
      health=$(curl -s -o /dev/null --write-out &quot;%{http_code}&quot; http://localhost:8080/health);
      if [[ ${used} -gt ${thresh} || ${health} -ne 200 ]]; then exit 1; fi
  initialDelaySeconds: 240
  periodSeconds: 60
  failureThreshold: 3
  timeoutSeconds: 10
</code></pre>
<p>You're already telling the YAML parser it's a multiline string</p>
