Hey everyone,
I run a lot of scripts via cron, even on my laptop, but my laptop isn't always connected to the internet. This can cause issues for scripts that need an online connection to do their job. To solve this, I wrote a simple helper function called is_online
that I can use at the start of my scripts to check for an internet connection before proceeding. It's a small but useful way to make my automated tasks more reliable.
The function works by using a clever and efficient trick that many modern operating systems use for their own "captive portal" checks. Instead of pinging a server or downloading a full webpage, it makes a request to a special URL that is designed to return an HTTP 204 status code.
A 204 No Content
response is unique because it tells the client that the request was successful, but there's intentionally no content to return. The server doesn't send a body, not even an empty one. This is perfect for a connectivity check because it confirms a successful connection with minimal data usage and resource cost.
is_online
FunctionHere is the simple Bash function I use:
# Checks if there is an internet connection.
is_online() {
local url="http://google.com/generate_204"
# use TLS if privacy matters
# local url="https://cp.cloudflare.com/generate_204"
local timeout=1
local response
response=$(
curl \
--output /dev/null \
--write-out "%{http_code}" \
--max-time "$timeout" \
--silent \
"$url"
)
if [ "$response" = "200" ] || [ "$response" = "204" ]; then
return 0
else
return 1
fi
}
This function uses curl
to make a request to Google's connectivity check URL and only outputs the HTTP response code. If the code is 204
(or 200
, as some proxies might return), the function returns a successful exit code (0
). Otherwise, it returns an error code (1
).
if is_online; then
echo "Online"
echo "Do stuff"
else
echo "Offline"
fi
Several major companies provide these generate_204
endpoints. Here are a few alternatives:
connectivitycheck.gstatic.com
.cp.cloudflare.com/generate_204
.edge-http.microsoft.com/captiveportal/generate_204
, particularly for the Edge browser.connectivitycheck.grapheneos.network/generate_204
.By adding this simple check to my scripts, I can ensure they only run when they're supposed to, preventing a backlog of errors when my laptop is offline.
As always,
Michael Garcia a.k.a. TheCrazyGM
That's a brilliant automation refinement! I love it! I automate various tasks on my systems as well, and several of them do require a working internet connection, so this is gold! Thank you so much as always for your digital wizardry! 😁 🙏 💚 ✨ 🤙
Hello thecrazygm!
It's nice to let you know that your article will take 10th place.
Your post is among 15 Best articles voted 7 days ago by the @hive-lu | King Lucoin Curator by keithtaylor
You receive 🎖 0.4 unique LUBEST tokens as a reward. You can support Lu world and your curator, then he and you will receive 10x more of the winning token. There is a buyout offer waiting for him on the stock exchange. All you need to do is reblog Daily Report 742 with your winnings.
Buy Lu on the Hive-Engine exchange | World of Lu created by szejq
STOP
or to resume write a wordSTART