希望每10秒檢查一次 Server 的 http code, 遇到不是 200 的時候,重啟 Server, 範例程式如下:
#!/bin/bash
while true
do
STATUS=$(curl --max-time 10 -k -s -o /dev/null -w '%{http_code}' https://127.0.0.1:443//status)
if [ $STATUS -eq 200 ]; then
echo "Got 200! All done!"
break
else
echo "Got $STATUS :( Not done yet..."
fi
sleep 10
done
執行畫面:
如果遇到 -eq 200 會出錯,可以改用
if [ $STATUS == “200” ]; then
備註: --max-time 10
指 curl 指令 timeout 為 10 秒。
Timeouts
Network operations are by their nature rather unreliable or perhaps fragile operations as they depend on a set of services and networks to be up and working for things to work. The availability of these services can come and go and the performance of them may also vary greatly from time to time.
The design of TCP even allows the network to get completely disconnected for an extended period of time without it necessarily getting noticed by the participants in the transfer.
The result of this is that sometimes Internet transfers take a very long time. Further, most operations in curl have no time-out by default!
Maximum time allowed to spend
Tell curl with -m / --max-time
the maximum time, in seconds, that you allow the command line to spend before curl exits with a timeout error code (28). When the set time has elapsed, curl will exit no matter what is going on at that moment—including if it is transferring data. It really is the maximum time allowed.
The given maximum time can be specified with a decimal precision; 0.5
means 500 milliseconds and 2.37
equals 2370 milliseconds.
Example:
curl --max-time 5.5 https://example.com/
Never spend more than this to connect
--connect-timeout
limits the time curl will spend trying to connect to the host. All the necessary steps done before the connection is considered complete have to be completed within the given time frame. Failing to connect within the given time will cause curl to exit with a timeout exit code (28).
The given maximum connect time can be specified with a decimal precision; 0.5
means 500 milliseconds and 2.37
equals 2370 milliseconds:
curl --connect-timeout 2.37 https://example.com/
Transfer speeds slower than this means exit
Having a fixed maximum time for a curl operation can be cumbersome, especially if you, for example, do scripted transfers and the file sizes and transfer times vary a lot. A fixed timeout value then needs to be set unnecessarily high to cover for worst cases.
As an alternative to a fixed time-out, you can tell curl to abandon the transfer if it gets below a certain speed and stays below that threshold for a specific period of time.
For example, if a transfer speed goes below 1000 bytes per second during 15 seconds, stop it:
curl --speed-time 15 --speed-limit 1000 https://example.com/
Keep connections alive
curl enables TCP keep-alive by default. TCP keep-alive is a feature that makes the TCP stack send a probe to the other side when there’s no traffic, to make sure that it is still there and “alive”. By using keep-alive, curl is much more likely to discover that the TCP connection is dead.
Use --keepalive-time
to specify how often in full seconds you would like the probe to get sent to the peer. The default value is 60 seconds.
Sometimes this probing disturbs what you are doing and then you can easily disable it with --no-keepalive
.
相關文章:
http://linux.vbird.org/linux_basic/0340bashshell-scripts.php#ifthen
語法:
# 一個條件判斷,分成功進行與失敗進行 (else) if [ 條件判斷式 ]; then 當條件判斷式成立時,可以進行的指令工作內容; else 當條件判斷式不成立時,可以進行的指令工作內容; fi |
如果考慮更複雜的情況,則可以使用這個語法:
# 多個條件判斷 (if ... elif ... elif ... else) 分多種不同情況執行 if [ 條件判斷式一 ]; then 當條件判斷式一成立時,可以進行的指令工作內容; elif [ 條件判斷式二 ]; then 當條件判斷式二成立時,可以進行的指令工作內容; else 當條件判斷式一與二均不成立時,可以進行的指令工作內容; fi |