How to Set Connection Timeout in Wget?
When downloading files using the wget command-line
utility, network interruptions or unresponsive servers can cause the
process to hang indefinitely. To prevent this, you can configure
explicit connection timeout limits using specific command-line flags.
This article provides a quick overview of how to use the
--connect-timeout option, combine it with read and total
timeout settings, and apply these configurations globally for all future
downloads.
The Basic Connection Timeout Command
The primary flag used to limit the time wget waits to
establish a connection with a remote server is
--connect-timeout. You specify the time limit in seconds
immediately following the flag.
To set a connection timeout limit of 10 seconds, use the following syntax:
wget --connect-timeout=10 https://example.com/file.zipIf the server fails to respond and establish a network connection
within 10 seconds, wget will abort the attempt rather than
waiting indefinitely.
Comprehensive Timeout Settings
Setting a connection timeout only covers the initial handshake. If
the connection is successful but the server stops sending data
mid-download, wget might still stall. To handle all types
of network freezes, you can use additional timeout flags:
- Read Timeout (
--read-timeout): Sets the maximum time to wait for new data to arrive once the connection is already active. - General Timeout (
-Tor--timeout): A blanket setting that applies the same limit to connection setup, read times, and DNS lookups simultaneously.
For example, to set a universal 15-second limit across all stages of the download process, use the shortcut flag:
wget -T 15 https://example.com/file.zipMaking Timeout Limits Permanent
If you frequently work with unreliable networks, you can avoid typing
these flags every time by saving your preferences in the
wget configuration file.
- Open your user configuration file located at
~/.wgetrc(or create it if it does not exist) using a text editor. - Add your desired timeout limits to the file:
connect_timeout = 10
read_timeout = 15
- Save and close the file.
Once these lines are added, wget will automatically
apply these timeout rules to every download command you run in the
future.