This command continuously pings google.com and logs any connection failures. It's useful for monitoring network connectivity and identifying intermittent issues.
while true; do
if ! ping -c 1 -W 5 google.com &> /dev/null; then
echo "$(date): Connection to google.com failed"
fi
sleep 1
done
-
if !
: Starts an if statement that executes the following block if the command returns a non-zero exit status (i.e., if it fails). -
ping
: The command used to test the reachability of a host on an IP network. -
-c 1
:- Argument:
-c
- Value:
1
- Meaning: Tells ping to send only one packet. The 'c' stands for "count".
- Argument:
-
-W 5
:- Argument:
-W
- Value:
5
- Meaning: Sets the timeout to 5 seconds. The uppercase 'W' stands for "timeout" and waits longer than the lowercase 'w'.
- Argument:
-
google.com
: The target host to ping. -
&> /dev/null
: Redirects both standard output and standard error to /dev/null, effectively silencing all output from the ping command. -
;
: Separates the ping command from thethen
keyword that follows. -
then
: Indicates the beginning of the code block to be executed if the ping fails.