Linux Commands

How to Prevent Timeouts in Curl

When transferring the data across a server, you can utilize Curl. It is an open-source and cross-platform tool that supports numerous protocols including HTTPS and HTTP. Curl works on all platforms. However, you may encounter a case where Curl runs into timeouts when connecting to the server which leads to dropped connections. If you have such a case, this post guides you on how to prevent timeouts in Curl. Read on!

Understanding Curl Timeouts

When Curl executes a request such as an HTTP request, the maximum duration that it takes to await a given event is the timeout period. The timeouts are ideal to control the time that Curl takes to establish a connection, await a response, or even transfer data. The Curl timeout is set using the –max-time or –connect-timeout parameters.

There are different Curl timeouts that you may encounter. For instance, a connection timeout determines the duration that Curl waits before the server establishes a TCP connection. If Curl fails to establish the connection within the set time, the connection gets aborted. We will see how to prevent this in the next section.

How to Prevent Timeouts in Curl

Timeouts can affect your connection and hinder you from executing your tasks. If you are having a rough time with timeouts, there is a way that you can prevent them. By setting the accurate timeout values, you will manage to control how long does Curl waits for requests to avoid delays. By defining the timeouts, you will manage to control how Curl handles the HTTP requests.

There are two main ways of preventing timeouts in Curl:

1. –connect-timeout <seconds>

In the case of connection timeout, you can extend the timeout period in seconds which Curl takes to establish a connection or abort it in case of no response. With the –connect-timeout option, you set your timeout in seconds with the following syntax:

curl --connect-timeout <seconds> <url>

Here’s an example where we set the timeout to 20 seconds:

The duration that you set is the maximum time it takes before your connection gets aborted. That way, you will prevent timeouts when using Curl.

2. –max-time <seconds>

An alternative way to prevent timeouts in Curl is to specify the total timeout that Curl should wait for the connection and transfer to complete. This total timeout is set using the –max-time option with the following syntax:

curl --max-time <seconds> <url>

For instance, we can have a –max-time of 30 seconds as illustrated in the following:

Thus, it takes 30 seconds before Curl can experience any timeout. Thirty seconds is enough time for the connection to occur, and you will prevent any timeout.

You can also set a retry script on timeout. A retry occurs when no connection is established. This option helps you to prevent a timeout by repeating the connection. You will end up with a case of a loop in your script. You can have the loop a few times, such as four times with a timeout interval of 5 seconds.

How you prevent timeouts in Curl depends on the timeout that you want to prevent. Generally, the two options are the standard ways of preventing timeouts.


Timeouts in Curl occur when a connection to the server fails to go through and gets aborted. You can prevent these timeouts by setting up the connection timeout duration and the maximum time before the connection gets dropped. We discussed the matter and the given examples of the two approaches. Hopefully, you can implement the same on your end and prevent timeouts in Curl.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.