Read Timeout vs Connect Timeout in Wget
When downloading files using the wget command-line tool,
network disruptions can cause your downloads to stall or fail. To handle
these situations gracefully, wget utilizes two distinct
timeout parameters: connect timeout and read
timeout. While both are designed to prevent the tool from
hanging indefinitely, they trigger at completely different stages of the
network request process. Understanding the difference between them
ensures your automated scripts and downloads handle network instability
efficiently.
What is Connect Timeout?
The connect timeout determines how long wget will wait
to establish a fundamental connection with the destination server. When
you initiate a download, wget must first perform a “TCP
handshake” to open a communication channel with the host.
- The Mechanism: If the remote server is offline, protected by a strict firewall that drops packets, or experiencing heavy network congestion, the connection phase may stall.
- The Flag: You can configure this limit using the
--connect-timeoutflag. - The Outcome: If
wgetcannot successfully establish the connection within the specified number of seconds, it abandons the attempt and returns a connection error.
What is Read Timeout?
The read timeout comes into play after a successful
connection has already been established with the server. Once connected,
wget expects a continuous stream of data from the host.
- The Mechanism: The read timeout specifies the
maximum amount of time
wgetwill wait for the server to send the next chunk of data. If the server suddenly stops transmitting data mid-download—perhaps due to a database error, server-side script crash, or an interrupted route—the read timeout clock starts ticking. - The Flag: This is controlled using the
--read-timeoutflag. - The Outcome: If the data stream idles for longer
than the specified limit,
wgetconsiders the read to have failed. It will then either abort or attempt to resume the download, depending on your retry configuration.
Key Differences at a Glance
The fundamental differences between these two timeout settings can be broken down by their timing, default behaviors, and primary causes:
| Feature | Connect Timeout (--connect-timeout) |
Read Timeout (--read-timeout) |
|---|---|---|
| Phase | Before the connection is established. | After the connection is established. |
| Trigger Event | Failure to complete the TCP handshake. | Failure to receive new data chunks mid-stream. |
| Primary Cause | Server is offline, wrong IP, or blocked by a firewall. | Server crashed mid-process, or network dropped during transit. |
| Wget Default | No default timeout (relies on the OS system timeout). | 90 seconds. |
Combining Options for Reliable Scripts
In unstable network environments, relying on just one of these settings can leave your scripts vulnerable to hanging. For the most robust automation, it is best practice to define both timeouts explicitly in your command.
wget --connect-timeout=10 --read-timeout=15 https://example.com/largefile.zipIn this example, wget will give up quickly (10 seconds)
if the server is completely unreachable, but will allow a slightly
longer grace period (15 seconds) if the data stream temporarily stutters
during the actual file transfer.