我正在使用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脚本的了解,我无法解决此问题。谁能指出我出了什么问题?
谢谢
答案 0 :(得分:1)
小修改:
美元符号 $ 应该转义-> \ $
“ \ $ 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
"