Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Curl Limit Request Time: How to Set Timeouts

· 4 min read · Home Tech

In this article
  1. What the Timeout Options Actually Do
  2. Why curl Has No Default Timeout
  3. How to Set the Timeout in Different Contexts
  4. Choosing the Right Timeout Value
  5. Common Problems and Misconceptions
  6. Comparing Timeout Options in Other Tools
  7. Safety and Best Practices for Scripts
  8. Frequently Asked Questions

The short answer: curl has no default request time limit, so a transfer can hang indefinitely unless you set one. Use the --max-time option (or -m) to cap the total request time in seconds, and --connect-timeout to limit just the connection phase.

What the Timeout Options Actually Do

curl offers two distinct timeout controls, and mixing them up is the most common mistake. --max-time limits the entire operation, from the moment the command starts until the last byte is received or sent. If the transfer takes longer than the specified seconds, curl aborts with exit code 28. --connect-timeout only limits the time spent establishing the TCP connection (and TLS handshake if HTTPS). Once connected, it has no effect.

For example, curl --max-time 10 https://example.com gives the whole request 10 seconds. curl --connect-timeout 5 https://example.com allows 5 seconds to connect, but then lets the download run forever. You can combine both: curl --connect-timeout 5 --max-time 30 https://example.com.

Why curl Has No Default Timeout

By default, curl waits indefinitely. The original design philosophy is that a transfer should complete when the server finishes sending, regardless of how long that takes. This is fine for interactive use, but problematic for scripts and automated jobs. A stalled server, a network black hole, or a misconfigured proxy can leave your command hanging for hours. In practice, most system administrators and developers set an explicit timeout in every non-interactive curl invocation.

How to Set the Timeout in Different Contexts

The most direct way is the command line, but you often need it in code or config files. Here are the common places:

  • Command line: Use -m 30 or --max-time 30.
  • curl config file: Put max-time = 30 in your ~/.curlrc or a custom config file, then use curl --config myconfig.
  • PHP (cURL extension): Set CURLOPT_TIMEOUT to seconds (integer) or CURLOPT_TIMEOUT_MS for milliseconds. Also set CURLOPT_CONNECTTIMEOUT.
  • Python (requests library): Pass timeout=(connect, read) as a tuple, e.g., requests.get(url, timeout=(5, 10)).
  • Node.js (axios): Use axios.get(url, { timeout: 10000 }) (milliseconds).

Note that in some languages the timeout applies to each read operation, not the whole request, so test carefully.

Choosing the Right Timeout Value

There is no one-size-fits-all number. A good starting point is 30 seconds for a typical API call, but consider the following:

  • Network conditions: A slow mobile connection may need 60 seconds or more.
  • Server processing time: If the server performs heavy computation, give it enough time.
  • File size: Large downloads need longer max-time, but you can also use --limit-rate to throttle speed and then calculate a safe timeout.
  • Retry logic: If you plan to retry on timeout, keep each attempt short (e.g., 10 seconds) and retry a few times.

For automated scripts, a common pattern is to set connect-timeout to 5–10 seconds and max-time to 30–60 seconds. Adjust based on your worst-case acceptable wait.

Common Problems and Misconceptions

One frequent issue is that --max-time does not apply to DNS resolution. If DNS is slow, it can still hang beyond your timeout. Use --dns-servers or system-level DNS timeouts to handle that. Another misconception is that --timeout exists in curl—it doesn't. The option is --max-time (or -m). Also, exit code 28 specifically means operation timeout, which is useful for scripting.

When using --max-time with --retry, note that the timeout applies to each attempt, not the total including retries. So --max-time 10 --retry 3 could take up to 40 seconds (plus retry delays).

Comparing Timeout Options in Other Tools

If you are used to other HTTP clients, the naming differs. Wget uses --timeout for network operations (connect, read, write) and --wait for delays between retries. In Python's requests, the timeout parameter is not a total time but a tuple of connect and read timeouts. In Node's native http module, you set timeout on the request object, but it only applies to inactivity, not total time. Understanding these differences helps when porting scripts.

Safety and Best Practices for Scripts

When writing cron jobs or CI pipelines, always set a timeout. A hanging curl can block a deployment or fill up logs. Use the --fail option to make curl return an error on HTTP 4xx/5xx, and combine it with --max-time. Also consider using --retry with --retry-delay to handle transient failures, but keep the total time in mind. For critical operations, log the exit code and the time taken.

Frequently Asked Questions

What is the default timeout for curl?

There is no default timeout. curl will wait indefinitely unless you specify one with --max-time or --connect-timeout.

How do I make curl timeout after 5 seconds?

Use curl --max-time 5 https://example.com. For connection only, use --connect-timeout 5.

Why does curl still hang even with --max-time?

Possible reasons: DNS resolution is not covered by --max-time, or the server is sending data slowly but continuously, which resets the inactivity timer. Also, if you use --retry, each attempt gets its own timeout.

What exit code does curl return on timeout?

Exit code 28 indicates operation timeout. You can check it in scripts with if [ $? -eq 28 ]; then ....

Can I set a timeout in a curl config file?

Yes. Add max-time = 30 to a config file and use curl --config myconfig. This is useful for consistent settings across many commands.