Kubernetes cron job call curl in loop

0 votes

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.

Here is my job yaml:

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
          - >
            apk update && apk add --no-cache curl bash && 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

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:

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

What's the problem?

Sep 17, 2018 in Kubernetes by Hannah
• 16,220 points
1,192 views

1 answer to this question.

0 votes

It's a small silly mistake:

dollar sign $ should be escaped -> \$

"\$exit_status" in place of $exit_status

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
            "
answered Sep 17, 2018 by Kalgi
• 41,990 points

Related Questions In Kubernetes

0 votes
1 answer

Run a cron job once in N hours

If you want your cron job to ...READ MORE

answered Sep 18, 2018 in Kubernetes by Kalgi
• 41,990 points
39 views
0 votes
3 answers

Change the schedule of Kubernetes cron job

kubectl patch <backup-cronjob> -p '{"spec":{"schedule": "0 0 ...READ MORE

answered Jun 20 in Kubernetes by sudhams reddy duba
463 views
+1 vote
4 answers
+5 votes
2 answers

Redirecting host to service path in kubernetes

What you are trying to do is ...READ MORE

answered Mar 27, 2018 in Kubernetes by DragonLord999
• 8,380 points
111 views
0 votes
1 answer
0 votes
3 answers

Error while joining cluster with node

Hi Kalgi after following above steps it ...READ MORE

answered Jan 17 in Others by anonymous
2,966 views
+3 votes
1 answer
0 votes
1 answer

Call endpoint periodically from kubernetes cron jon

Cron job would be appropriate to use. ...READ MORE

answered Sep 17, 2018 in Kubernetes by Kalgi
• 41,990 points
49 views
0 votes
1 answer

Connect to existing pod, execute script, disconnect - Kubernetes cron job

As far as i know there's no ...READ MORE

answered Sep 17, 2018 in Kubernetes by Kalgi
• 41,990 points
862 views