gpt4 book ai didi

bash - Kubernetes cron作业循环调用curl

转载 作者:行者123 更新时间:2023-12-02 12:06:58 26 4
gpt4 key购买 nike

我正在使用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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com