I am trying to a write one liner for sending an alert email if the time taken for curl command exceeds 1 sec.
if time curl {URI} > 1; then mailx -r [email protected] -s ALERT; fi
How since time command returns more than just a numeric value, this fails.
Any ideas on how I can achieve this?
From the look of it, the result of curl does not seem to matter.
So I propose to kill the command if it spans more than one second by using timeout. Here is the one-liner:
If it timeouts, it returns an error and the mailx command is run.
No tested but that should work.
I'd save the PID of the
curl
process,sleep
a second and just test whether it's still running withkill
:You can test this easily with
sleep 1
orsleep 2
respectively:Curl has an option for this, --max-time (and --connect-time, if you want to control time spend in the connect phase on the session). If timeout occurs, curl will return error-code 28.
So something like this:
should do it.
I found this site Everything Curl which describes the capabilities of Curl.