cURL by Example: Timeout Settings
Set connection and operation timeouts. Prevents cURL from hanging indefinitely.
Code
# Maximum time allowed for the connection to be established
curl --connect-timeout 10 https://example.com
# Maximum time allowed for the entire operation
curl --max-time 30 https://example.com
# Combine both for robust scripts
curl --connect-timeout 5 --max-time 20 https://api.example.comExplanation
Timeout configuration is absolutely critical when writing robust scripts and automation. Without proper timeouts, cURL can hang indefinitely waiting for unresponsive servers, causing your entire script, application, or CI/CD pipeline to stall. This is particularly problematic in automated cron jobs, monitoring systems, health checks, and batch processing where a single stuck request can block an entire queue. cURL provides two complementary timeout mechanisms that address different phases of the HTTP request lifecycle, and using both together provides comprehensive protection against hangs.
Connection Timeout (--connect-timeout): This option specifies the maximum time in seconds that cURL will wait during the initial connection establishment phase. This phase includes DNS resolution (looking up the server's IP address), TCP handshake (the three-way SYN, SYN-ACK, ACK exchange), and for HTTPS, the TLS/SSL handshake (certificate exchange and cipher negotiation). Once the connection is successfully established, this timeout no longer applies. The syntax is --connect-timeout <seconds>, and you can use decimal values for sub-second precision, for example --connect-timeout 2.5 for 2.5 seconds. If cURL cannot establish a connection within the specified time, it terminates with an error. This timeout is your first line of defense against network issues: DNS servers not responding, server firewalls dropping packets, servers that are completely down, or severely congested network routes. A typical value for production systems is 5-10 seconds; anything longer suggests serious network or server issues worth investigating.
Total Operation Timeout (--max-time or -m): This sets the maximum time allowed for the entire cURL operation from start to finish, including all phases: DNS resolution, connection establishment, sending the request, and receiving all response data. The operation is forcefully terminated if this limit is exceeded, regardless of progress. This is your safeguard against slow data transfers, stalled downloads, servers that accept connections but never send data, extremely large responses that take too long to download, or server-side processing that hangs. If a timeout occurs, cURL exits with error code 28 (CURLE_OPERATION_TIMEDOUT), which your scripts can detect and handle appropriately. Both timeout values support fractional seconds (e.g., -m 0.5 for 500 milliseconds), making them suitable for high-performance scenarios requiring strict SLA compliance.
Why Use Both Together: Using only --connect-timeout protects against connection failures but won't help if the server accepts the connection and then hangs during data transfer. Using only --max-time might allow too much time to be wasted on the connection phase when the server is clearly unreachable. The recommended approach is --connect-timeout 5 --max-time 20: fail fast (within 5 seconds) if we can't even connect, but allow up to 20 seconds total for the complete operation including a potentially slow data transfer. This gives a responsive failure for truly dead servers while being tolerant of slow but functional services. For health checks and monitoring, use shorter timeouts (1-3 seconds) to quickly detect issues. For large file uploads/downloads, use longer max-time values but keep connect-timeout relatively short. Always include these flags in production automation, cron jobs, CI/CD pipelines, and any unattended scripts to prevent indefinite hangs.
Code Breakdown
--connect-timeout 10 waits 10s for the handshake.--max-time 30 kills the process after 30s regardless of progress.
