linux shell curl 超時(shí)與重試
curl 關(guān)于時(shí)間控制和重試的參數(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í)參數(shù) connect-timeout
--connect-timeout SECONDS Maximum time allowed for connection
示例
#這里我們設(shè)置超時(shí)時(shí)間為2s, 請求一個(gè)無法解析的地址 curl --connect-timeout 2 --url http://xxx.com curl: (28) Connection timed out after 2002 milliseconds
顯示連接超時(shí), 超時(shí)時(shí)間2002毫秒. 注意這個(gè) warning 的時(shí)間可能每次統(tǒng)計(jì)不太一樣, 一般會超過我們的預(yù)設(shè)值一點(diǎn).
#對于一個(gè)對返回時(shí)間要求比較高的情況, 可以設(shè)置為浮點(diǎn)型精確到毫秒 curl --connect-timeout 0.3 --url http://xxx.com curl: (28) Connection timed out after 300 milliseconds
請求超時(shí)時(shí)間 --max-time
-m, --max-time SECONDS Maximum time allowed for the transfer
示例
#這里我們設(shè)置超時(shí)時(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
#這里我們使用了一個(gè)無法解析的地址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)フ埱笠粋€(gè) 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
重試超時(shí)時(shí)間 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è)置重試時(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)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請聯(lián)系工作人員刪除。