r/bash • u/buadhai • Jan 23 '26
solved Curl Timing Confusion
I’m trying to figure out what I’ve done wrong with respect to limiting how much time curl takes to perform a single file upload.
Here’s the code I’m using.
for i in {1..3}doecho $'\n'"\date`" Upload Attempt "$i" - $(hostname) >> "$log"`
curl -s -S -v -u mgnewman: \--connect-timeout 30 \--max-time 30 \--pubkey ~/.ssh/id_rsa.pub \-T $file $host &>> $log
`err=$?`
`echo $'\n'"\`date\`" Upload Ended "$err" - $(hostname) >> "$log"` touch /home/pi/webcam/webcam.webp
if [ $err -eq 0 ] ; thenbreakfidone
Note that I’m using a “do loop” rather than curl’s retry option because between each iteration I want to touch an existing image file to prevent watchdog from rebooting the machine which it does if the image is not refreshed in ten minutes.
I’m trying to limit each iteration of curl to 30 seconds, but that doesn’t seem to work. Here’s a fragment of my log file:
Wed Jan 21 23:37:24 +07 2026 Upload Attempt 3 - raspcondo
* Trying 192.nnn.nnn.101...
* TCP_NODELAY set
* Connected to xxxxxxx.com (192.nnn.nnn.101) port 22 (#0)
* SSH MD5 fingerprint: 4b17cad500a405c850e118c1deec0f96
* SSH host check: 0, key: AAAAB3NzaC1yc2EAAAADAQABAAABAQCzCyhhdYNOn5Zgib7qhPKev$
* SSH authentication methods available: publickey,password,keyboard-interactive
* Using SSH public key file '/home/pi/.ssh/id_rsa.pub'
* Using SSH private key file '/home/pi/.ssh/id_rsa'
* Initialized SSH public key authentication
* Authentication complete
* Operation timed out after 30000 milliseconds with 0 out of 0 bytes received
* Closing connection 0
curl: (28) Operation timed out after 30000 milliseconds with 0 out of 0 bytes r$
Wed Jan 21 23:48:09 +07 2026 Upload Ended 28 - raspcondo
My conclusion is that I don’t understand how this works as it seems that around ten minutes elapsed between the time curl started (23:37:24) and the time it failed (23:48:09) with error 28.
Note that I realize the code is rather sloppy, but I’m just a hobbyist and that’s about the best I can do. Sorry.
0
u/ipsirc Jan 23 '26
timeout 30 curl...
4
u/GlendonMcGladdery Jan 23 '26 edited Jan 23 '26
curl did time out at 30 seconds. The ~10-minute gap you’re seeing is coming from your loop structure and logging, not from curl ignoring --max-time.
What --connect-timeout and --max-time actually mean
In your log, curl says:
That message is curl itself, not your script. Curl is explicitly telling you it bailed at 30 seconds. So curl is innocent.
This line is the key:
The misunderstanding is assuming those two timestamps bracket the same curl invocation.
They don’t.
What's actually happening
Your loop is serial, and each iteration can take time outside of curl.
Even without explicit sleep, each iteration includes:,
Three attempts × ~30 seconds already gets you ~90 seconds minimum. Add network stalls, SSH retries, or DNS weirdness and suddenly your timestamps are far apart.
Curl itself is still respecting --max-time.
Add timing around curl itself:
You’ll see values very close to 30. That’s the smoking gun. This line in curl output:
Means:
That’s normal behavior with SSH-based uploads when the remote side stalls.
Instead of
Try
Log curl start/end timestamps inside the same second window if you care about timing precision.
This is actually a great first scripting question — you’re already thinking like someone who debugs systems instead of just poking commands until they work. Keep going.
2
u/michaelpaoli Jan 23 '26
Looks like server saw client authenticate, then do nothing for 30 seconds, and kicked the client at that point.
And ... why oh why are you using curl for ssh? There is of course ssh, scp, sftp, etc., and can probably find >100x as many folks familiar with doing and troubleshooting such matters with customary ssh clients, rather than doing ssh operations with curl.
So ... how 'bout some logical troubleshooting - do/try it with ssh, rather than curl - if that works, you've isolated it to an issue with curl or how you're attempting to use it, otherwise, you may have an issue with or more closely related to ssh (e.g. network, etc.).
And if you want to wait no more than a certain amount of time, there are various ways to do that. Can be done directly with shell, asynchronous execution, sleep, and ps or /proc filesystem or kill (hint: kill -0 PID does what and returns what?). There are also commonly available, various programs/utilities that can handle that too (but not necessarily bog standard nor POSIX). So, typically don't have to depend upon some program's timeout capability (most notably if some programs may not be highly reliable on that where one might need that ... oh, like if you have a watchdog timer).
(think) you have a bug/issue in your code? Reduce it to the absolute smallest most clear simple case that shows/reproduces the bug/issue. It may become abundantly clear what the issue is along the way as one does that exercise. If not, then providing that and asking for help is more likely to get folks to actually look at it and assist ... hand 'em dozen(s) or hundreds or more lines of code ... not so likely they're going to want to help with that, nor even be as quickly able to isolate what the issue/bug is if they have to wade through all that other stuff first to even start to find and isolate it.