BudiBadu Logo
Samplebadu

cURL by Example: Max Time Limit

Latest

Hard limit on operation duration. Synonymous with `--max-time` but focused on use cases.

Code

# Fail if the request takes longer than 1 second
curl -m 1 https://api.fast-service.com/status

# Allow a long download but cap it at 1 hour (3600 seconds)
curl -m 3600 -O https://example.com/huge-backup.tar.gz

Explanation

The -m flag (short form of --max-time) is your ultimate safety mechanism to ensure cURL operations terminate within a predictable timeframe. This flag sets an absolute upper bound on how long any single cURL operation can run, measured in seconds from the moment the command starts until it either completes or is forcefully terminated. When the time limit is exceeded, cURL immediately aborts the operation and returns exit code 28 (CURLE_OPERATION_TIMEDOUT), which allows your scripts to programmatically detect timeouts and implement retry logic, error handling, or alerting as needed.

Strategic Use Cases: Different scenarios require different timeout philosophies. For health checks and monitoring, very short timeouts (1-3 seconds) are appropriate because a slow response is often as problematic as no response—if an API endpoint takes longer than your SLA allows, it should be considered failed even if it eventually responds. Example: curl -m 2 https://api.example.com/health ensures the health check completes within 2 seconds or is marked as failed. For batch processing and job queues, you don't want one stuck request to block the entire queue. Setting reasonable timeouts ensures jobs keep moving: curl -m 30 https://api.example.com/process. For large file downloads, you need generous timeouts but still want protection against completely stalled transfers. A 1-hour timeout (curl -m 3600 -O https://cdn.example.com/huge-file.zip) prevents indefinite hangs while allowing legitimate large downloads. For real-time applications where latency matters, strict sub-second timeouts enforce performance requirements: curl -m 0.5 for a 500ms SLA.

Timeout vs Speed Limits: It's important to understand that --max-time doesn't control how fast data transfers, but rather how long the operation is allowed to continue. A 10MB file might take 5 seconds on a fast connection or 50 seconds on a slow one. If you set -m 10, the download will succeed on the fast connection but fail on the slow one, regardless of eventual success. This makes --max-time best suited for scenarios where you care about responsiveness over completion, or where you're downloading predictably-sized resources over reliable connections. For unpredictable network conditions or file sizes, consider using --speed-time and --speed-limit instead, which abort transfers that fall below a minimum speed threshold for a sustained period.

Error Handling and Retry Logic: When implementing robust systems, always check cURL's exit code. Exit code 28 specifically indicates a timeout. Your scripts can catch this and implement exponential backoff retry logic: retry with longer timeouts, log the incident for monitoring, fall back to alternative endpoints, or alert operations teams. Combine with --retry and --retry-max-time for automatic retry behavior. Example defensive script pattern: curl -m 10 --retry 3 --retry-delay 2 --retry-max-time 40 https://api.example.com tries for 10 seconds, then retries up to 3 times with 2-second delays, but aborts the entire retry cycle after a total of 40 seconds. Fractional values are supported for high-precision requirements: -m 1.5 for exactly 1.5 seconds, useful in performance testing or SLA enforcement scenarios.

Code Breakdown

2
-m 1 enforces a strict 1-second SLA.
5
-m 3600 prevents a download from running indefinitely.
All
Returns exit code 28 on timeout.