Shell command retrieving output of curl not working

76 Views Asked by At

I am trying to execute this command through a Kubernetes livenessProbe, and I don't think it's working as expected as I see that the probe has failed in the pod events log. This is the command:

sh -c reply=$(curl -s -o /dev/null -w %{http_code} http://127.0.0.1:8080/login); if [ "$reply" -lt 200 -o "$reply" -ge 400 ]; then exit 1; fi;

When I exec into the container and run this command, I get this error:

/bin/sh: 1: [: Illegal number:

I echo $reply and nothing is stored in the reply variable after running the above command. When I run the command without the 'sh -c' part, it works and the variable reply is getting populated. Somehow, the sh command is not able to read the output of the curl command...I tried to remove the -o /dev/null part of the curl request and that does not help.

How can I get this command to execute?

2

There are 2 best solutions below

6
tax evader On

To run sh -c with multiple synchronous commands, you need to enclose them inside quotes (either " or ') and add backslash before any similar quotes using inside the command and before the dollar sign $ of the variable

sh -c "export reply=$(curl -s -o /dev/null -w %{http_code} http://127.0.0.1:8080/login); if [ \"\$reply\" -lt 200 -o \"\$reply\" -ge 400 ]; then exit 1; fi;"
2
SS123 On

Along with @tax evader's answer, I got the probe to work by putting this in the deployment spec:

livenessProbe:
  exec:
    command: ["sh", "-c", 'export reply=$(curl -s -o /dev/null -w %{http_code} http://127.0.0.1:8080/login); if [ \"\$reply\" -lt 200 -o \"\$reply\" -ge 400 ]; then exit 1; fi;'