<p>I am using kubernetes 1.10 cluster and I want to schedule cron job which will use bash script to loop forever and send get request to http endpoint in every two seconds.</p>

<p>Here is my job yaml:</p>

<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: notifsender-sms-cron
  namespace: staging
spec:
  template:
    spec:
      containers:
      - name: notifsender-sms-cron
        image: alpine:latest
        command: ["/bin/sh"]
        args:
          - -c
          - &gt;
            apk update &amp;&amp; apk add --no-cache curl bash &amp;&amp; bash -c 
            " echo \"Running cron loop\";
              while true; 
              do
                exit_status=$(curl -v -o /dev/null -w '%{http_code}' http://bbc.com);
                if [ $exit_status -ne 200 ]; then
                    exit 1;
                fi
                sleep 2;
              done
              "
      restartPolicy: OnFailure
  backoffLimit: 4
</code></pre>

<p>The problem is that output is unexpected, because curl request is made only once and even before the loop, than program loops without any curl request made:</p>

<pre><code>...
 &amp;gt; Accept: */*
 &amp;gt; 
 &amp;lt; HTTP/1.1 301 Moved Permanently
 &amp;lt; Server: Varnish
 &amp;lt; Retry-After: 0
 &amp;lt; Content-Length: 0
 &amp;lt; Accept-Ranges: bytes
 &amp;lt; Date: Thu, 05 Jul 2018 17:53:32 GMT
 &amp;lt; Via: 1.1 varnish
 &amp;lt; Connection: close
 &amp;lt; X-Served-By: cache-fra19122-FRA
 &amp;lt; X-Cache: MISS
 &amp;lt; X-Cache-Hits: 0
 &amp;lt; X-Timer: S1530813212.281242,VS0,VE0
 &amp;lt; Location: http://www.bbc.com/
 &amp;lt; cache-control: public, max-age=3600
 &amp;lt; 

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
* Closing connection 0
Running cron loop
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
</code></pre>

<p>I think I have mistake in script, But with my current knowledge of bash scripting I can't fix this problem. Can anyone point me what's wrong ?</p>

<p>Thank you</p>

Davit Tvildiani
<p>Minor modification:</p>

<p>dollar sign <strong>$</strong> should be escaped -> <strong>\$</strong></p>

<p>"<strong>\$exit_status</strong>" in place of $exit_status </p>

<pre><code>bash -c " echo \"Running cron loop\";
            while true; 
            do
            exit_status=\$(curl -v -o /dev/null -w '%{http_code}' http://bbc.com);
            if [ \$exit_status -ne 200 ]; then
                exit 1;
            fi
            sleep 2;
            done
            "
</code></pre>
