Set Timeout with wget

Set Timeout with wget

The wget is a command line tool for sending requests to the server. One of its features is the ability to set timeouts, preventing commands from running indefinitely if a server is unresponsive. This is especially useful when dealing with unreliable networks or services prone to latency. This tutorial shows how to set timeout with wget.

Connection timeout

The connection timeout determines how long wget should wait to establish a connection to the server before aborting the attempt. You can control this using the --connect-timeout option.

wget -O- -t1 --connect-timeout 0.1 https://httpbin.org/get

In this example, wget will wait up to 0.1 seconds for establishing a connection. If the connection isn't established within this timeframe, wget will terminate the attempt and return an error:

--2024-05-29 19:09:54--  https://httpbin.org/get
Resolving httpbin.org (httpbin.org)... 52.204.69.97, 52.206.26.65, 18.208.55.4, ...
Connecting to httpbin.org (httpbin.org)|52.204.69.97|:443... failed: Connection timed out.
Connecting to httpbin.org (httpbin.org)|52.206.26.65|:443... failed: Connection timed out.
Connecting to httpbin.org (httpbin.org)|18.208.55.4|:443... failed: Connection timed out.
Connecting to httpbin.org (httpbin.org)|34.198.16.126|:443... failed: Connection timed out.
Connecting to httpbin.org (httpbin.org)|52.6.174.184|:443... failed: Connection timed out.
Connecting to httpbin.org (httpbin.org)|52.5.142.200|:443... failed: Connection timed out.
Giving up.

The -t option specifies the number of retry attempts. In our case, it is set to 1.

Maximum Time

The maximum time option limits the total time allowed for the entire operation. This includes DNS lookup, connection time, read and write. It can be specified with --timeout option.

wget -O- -t1 --timeout 0.3 https://httpbin.org/get

In this example, wget will wait up to 0.3 seconds for the entire process. If the operation isn't completed within this time, wget will abort and return an error:

--2024-05-29 19:22:53--  https://httpbin.org/get
Resolving httpbin.org (httpbin.org)... 34.198.16.126, 52.5.142.200, 18.208.55.4, ...
Connecting to httpbin.org (httpbin.org)|34.198.16.126|:443... connected.
HTTP request sent, awaiting response... Read error (Connection timed out) in headers.
Giving up.

Leave a Comment

Cancel reply

Your email address will not be published.