The curl command is used to download files and web pages by using different protocols like HTTP, HTTPS, FTP, etc. As the curl command work over a network connection first phase of the download is connecting to the remote server. The remote server connection can take some time according to the network speed, latency, remote service load, etc. If there is a problem the curl can not connect or connect very late to the remote service. The wait time can be specified for the curl command. The wait time is called a timeout. The curl command has a default or implicit timeout value but we can also specify a custom or explicit timeout value for the curl command.
–connect-timeout Option
The –connect-timeout option is the most appropriate option in order to set a timeout for the curl command. This option specifies the connection phase timeout value. If the connection which is a TCP connection can not be established in a specified interval the curl command stops connection and operation. The –connection-option value specified as seconds. In the following example, we set the connection timeout as 3 seconds for the curl command.
curl --connection-timeout 3 https://linuxtect.com/
–connect-timeout For Milliseconds
The –connect-timeout option can be used to specify the connection timeout as milliseconds. From the curl version 7.32 the –connect-timeout value can be specified as decimal value where milliseconds can be specified. In the following example the timeout value is set as 3.5 seconds or 3 seconds 500 milliseconds.
curl --connection-timeout 3.5 https://linuxtect.com/
–max-time Option
If you need to specify the timeout for the whole operation the –max-time option can be used. The –max-time option is used to specify the whole operation time out and if the operation is not completed in the specified interval the operation is canceled. For example, if the curl command is used to download a file and starts download but does not complete according to the –max-time option value the operation is canceled. In the following example, we set the –max-time option as 10 seconds as the specified file is not big in size and network speed is good.
curl --max-time 10 https://linuxtect.com/file.txt