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
• 18,540 points
8,984 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
• 52,350 points

Related Questions In Kubernetes

+1 vote
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
• 52,350 points
842 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 21, 2019 in Kubernetes by sudhams reddy duba
7,337 views
+1 vote
4 answers

Execute shell script file using kubernetes as a cron job

use /bin/sh instead of /bin/bash This solved it ...READ MORE

answered May 7, 2019 in Kubernetes by Sid
17,714 views
+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,450 points
3,567 views
+1 vote
1 answer
0 votes
3 answers

Error while joining cluster with node

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

answered Jan 17, 2019 in Others by anonymous
15,437 views
+4 votes
1 answer

Installing Web UI (Dashboard):kubernetes-dashboard on main Ubuntu 16.04.6 LTS (Xenial Xerus) server

Follow these steps: $ kubeadm reset $ kubeadm init ...READ MORE

answered Apr 12, 2019 in Kubernetes by Kalgi
• 52,350 points

reshown Apr 12, 2019 by Kalgi 7,065 views
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
• 52,350 points
922 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
• 52,350 points
4,804 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP