bash - Kubernetes cron作业循环调用curl

标签 bash curl kubernetes alpine

我正在使用kubernetes 1.10集群,我想安排cron作业,它将使用bash脚本永久循环并每两秒钟将get请求发送到http端点。

这是我的工作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

问题在于输出是意外的,因为curl请求仅在循环之前甚至在循环之前发出一次,而不是程序循环而不发出任何curl请求:
...
 > 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

我认为我在脚本中有错误,但是以我目前对bash脚本的了解,我无法解决此问题。谁能指出我出了什么问题?

谢谢

最佳答案

小修改:

美元符号 $ 应该转义-> \ $

\ $ exit_status ”代替$ 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
            "

关于bash - Kubernetes cron作业循环调用curl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51197995/

相关文章:

deployment - 重新加载 Kubernetes 部署以在不停机的情况下获取新 key

kubernetes - Kubectl exec 超时

bash - 当在 Behat 上下文中作为 Symfony Process 运行时,使用 Expect 生成交互式 CLI 命令会提前以 0 退出

ruby - 通过 Ruby 和 redis-cli 将 JSON 负载推送到 redis

bash - shell命令中 "&&"的用途是什么?

php - 尝试通过 http api 将数据存储在外部 ipfs 节点上,ipfs.infura.io 拒绝我的连接

php - 加载所有javascript后如何输出DOM?

amazon-web-services - 我不完全理解容器化如何从一开始就不会导致过度配置实例

mysql - 如何在 bash 中的双引号内使用一对单引号?

rest - 如何通过 REST API 从 Bamboo Atlassian 构建计划中检索我的自定义变量