Shell Scripting: Proper way to check for internet connectivity?
42
votes
10
answers
149008
views
I found scripts that say they check for internet connectivity. Some check the IP address if the interface is up BUT it does not check for internet connectivity. I found some that uses ping like this:
if [ 'ping google.com -c 4 | grep time' != "" ]; then
but sometimes this may be unreliable as ping itself may hang for some reason (e.g. waiting for some stuck IO).
Any suggestions on the proper/reliable way to check for internet connectivity using scripts? Do I have to use some packages?
It needs to be able to check periodically with cron
for example, then, do something when the connection goes down like invoke ifup --force [interface]
Asked by Aloha
(2171 rep)
Mar 16, 2015, 03:28 PM
Last activity: Feb 13, 2025, 05:47 AM
Last activity: Feb 13, 2025, 05:47 AM