linux shell curl 超時與重試
curl 關(guān)于時間控制和重試的參數(shù)
curl --help --connect-timeout SECONDS Maximum time allowed for connection -m, --max-time SECONDS Maximum time allowed for the transfer ... --retry NUM Retry request NUM times if transient problems occur --retry-delay SECONDS Wait SECONDS between retries --retry-max-time SECONDS Retry only within this period
連接超時參數(shù) connect-timeout
--connect-timeout SECONDS Maximum time allowed for connection
示例
#這里我們設(shè)置超時時間為2s, 請求一個無法解析的地址 curl --connect-timeout 2 --url http://xxx.com curl: (28) Connection timed out after 2002 milliseconds
顯示連接超時, 超時時間2002毫秒. 注意這個 warning 的時間可能每次統(tǒng)計不太一樣, 一般會超過我們的預(yù)設(shè)值一點.
#對于一個對返回時間要求比較高的情況, 可以設(shè)置為浮點型精確到毫秒 curl --connect-timeout 0.3 --url http://xxx.com curl: (28) Connection timed out after 300 milliseconds
請求超時時間 --max-time
-m, --max-time SECONDS Maximum time allowed for the transfer
示例
#這里我們設(shè)置超時時間為2s, 應(yīng)用程序中sleep 2curl --max-time 2 --url http://www.shuai.comcurl: (28) Operation timed out after 2002 milliseconds with 0 bytes received
#這里我們使用了一個無法解析的地址curl --connect-time 3 --max-time 2 --url http://xxx.com> curl: (28) Connection timed out after 2001 millisecondscurl --connect-time 3 --max-time 4 --url http://xxx.com> curl: (28) Operation timed out after 4002 milliseconds with 0 bytes received
請求重試 retry
--retry NUM Retry request NUM times if transient problems occur
#同樣,我們?nèi)フ埱笠粋€ sleep 2 的地址curl --max-time 0.1 --retry 3 --url http://www.shuai.com> Warning: Transient problem: timeout Will retry in 1 seconds. 3 retries left.> Warning: Transient problem: timeout Will retry in 2 seconds. 2 retries left.> Warning: Transient problem: timeout Will retry in 4 seconds. 1 retries left.> curl: (28) Operation timed out after 100 milliseconds with 0 bytes received
重試超時時間 retry-max-time
curl --retry 3 --retry-max-time 2 --max-time 0.1 --url http://www.shuai.com> Warning: Transient problem: timeout Will retry in 1 seconds. 3 retries left.> Warning: Transient problem: timeout Will retry in 2 seconds. 2 retries left.> curl: (28) Operation timed out after 101 milliseconds with 0 bytes received
重試延遲 retry-delay
#這里我們設(shè)置重試時間5s,重試3次curl --retry 3 --retry-delay 5 --max-time 0.1 --url http://xxx.com> Warning: Transient problem: timeout Will retry in 5 seconds. 3 retries left.> Warning: Transient problem: timeout Will retry in 5 seconds. 2 retries left.> Warning: Transient problem: timeout Will retry in 5 seconds. 1 retries left.> curl: (28) Connection timed out after 101 milliseconds
*博客內(nèi)容為網(wǎng)友個人發(fā)布,僅代表博主個人觀點,如有侵權(quán)請聯(lián)系工作人員刪除。